Skip to content

u/pschart

In this archive from Dec 2009 to Apr 2018

0
posts
9
comments
0
media
33
score

And here I thought it meant something like, "turn away (back) now for being in a relationship with me is perilous and doomed to failure." Project much, do I?

1 points

It's short, sweet and to the point, but: How will you invoke it? Keyboard shortcut? Also, what if one of the items in "myFolder" has the same name as one of the items in "container of myFolder"? Mine (above) takes care of these problems albeit in a more roundabout kind of way. Saving it as a service lets you right-click a folder and choose it, it also tries each item individually and handles the possibility of duplicate names gracefully. My script is ugly, though! ;)

2 points

I took a bit of a different approach (which also handles multiple folder selections). I used Automator so I could make a new "Service" that accepts folders. This way it shows up in the context menu if you right-/control-click on a folder.

It's pretty easy, launch Automator and choose "Service" as the template. Choose "Selected Folders" from the "Service receives:" dropdown and "Finder" from the "in" dropdown. Drag a "Get Selected Finder Items" action to the workflow and then a "Run Applescript" one below it.

Here's the (quick and dirty) script I came up with:

on run {input, parameters}
	set errorTripped to false
	repeat with anAlias in input
		tell application "Finder"
			if kind of anAlias is "Folder" then --make sure the target item is a folder
				set dest to container of anAlias --the parent of the target, this is where we'll move the contents of 'anAlias'
				set theFiles to every item of anAlias --get all the files/folders in the target folder
				
				--move all files and folders up to the parent folder
				repeat with aFile in theFiles
					set itemKind to "file"
					if (kind of aFile is "Folder") then set itemKind to "folder"
					
					try
						move aFile to dest --try to move the current file
					on error
						--most likely reason this would error out is because an item with the same name as 'aFile' already exists there. We could prompt for a new name, or just append something to the name, but for simplicity's sake, we'll just note the error and not delete the target folder, leaving the duplicate named items where they were. 
						display dialog "Could not move " & itemKind & " '" & the (name of aFile) & "' to parent folder, most likely there is already a file/folder with the same name."
						set errorTripped to true
					end try
				end repeat
				
				--Make sure there were no errors and that the target folder is now empty
				if (errorTripped is false and (count of (every item of anAlias)) is 0) then
					delete anAlias
				end if
			end if
		end tell
	end repeat
	
	if (errorTripped is true) then
		display dialog "The target folder was not deleted because one or more items could not be moved to the parent folder."
	end if
end run

The script may be a bit sloppy, but doing it one file/folder at a time has the benefit of not bailing in the middle if a file/folder with the same name already exists in the parent directory. It will move every folder/file that has a "unique in the parent directory" name and only delete the "target" folder if no errors occurred and the folder is empty.

Disclaimer: this is quick and dirty, I tested it a bit but I am not responsible if you use it and it fucks something up! ;)

3 points

End of this list.

Back to top

Keyboard shortcuts

Move and act

jork
Next / previous post or comment
Enteroro
Open the post
f
Open the media viewer
s
Save
h
Hide (the toast has Undo)
Tab
Right after an action: the toast's Undo
x
Collapse the comment
v
Next layout: cards, compact, grid

Go to

/
Search
gthenh
Home
gthena
All
gthens
Subreddits
gthenl
Library
?
This list
Esc
Close a menu or this list

In the viewer

←or→
Previous / next file
↑or↓
Previous / next post
korj
Previous / next post
Space
Play / pause
m
Mute
z
Zoom
s
Save
o
Open the post (on its own page: back to it)
Esc
Close

Keys do nothing while you type in a box.