View Full Version : Move Selected Files to a New Folder
magilum
May 1st, 2006, 04:02 AM
Move Selected Files to a New Folder using Automator and AppleScript.
Launch Automator, select "Automator" from the Library sidebar, then double-click "Run AppleScript" from the Action sidebar. Select all in the script field and paste:
on run {input, parameters}
set the_folder to (choose folder)
tell application "Finder"
move selection to the_folder
end tell
return input
end run
Go to File>Save As Plug-In... and save it as a plug-in for the Finder.
This gives you a new contextual submenu that will appear under Automator when control/right-clicking in the Finder. Select what you want to move, then invoke the menu.
If you have folder or window within view that you want to move the file to, drag the folder (or a file within it) or the icon in the title bar of an open folder to the "Choose a Folder" dialog to jump to that location.
---
This method was compiled based on several results from sites and message board threads that turned up in a search.
barhar
May 1st, 2006, 08:09 AM
'Finder's 'move' method (function, handler) moves a selection from one location on a volume to another location - on the same volume.
'move' copies the selection, when the destination is on 'another' volume.
[MacOS X 10.4.4, AppleScript 1.10.3]
magilum
May 1st, 2006, 04:35 PM
You're right. I don't see anything in the dictionary about modifying this behavior.
However, in running the workflow, I can see that the file, when copied to a new volume rather than moved within on, remains selected. So, what if something was added to the script to compare the target and source volumes, and if they don't match, to delete the selected items?
barhar
May 1st, 2006, 06:38 PM
'Automator' contexual menu '.workflow' code ...
on run {input, parameters}
try
tell application "Finder" to set der_Auswahl to selection
if ((count der_Auswahl) = 1) then
set der_Auswahlpfad to my neuer_Pfad(POSIX path of (der_Auswahl as string))
else
set der_Auswahlpfad to {}
repeat with i in der_Auswahl
copy (my neuer_Pfad(POSIX path of (i as string))) to end of der_Auswahlpfad
end repeat
set {oAStID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, " "}
set der_Auswahlpfad to (der_Auswahlpfad as string)
set AppleScript's text item delimiters to (oAStID)
end if
set der_Pfad to my neuer_Pfad(POSIX path of (choose folder))
do shell script "mv " & der_Auswahlpfad & " " & der_Pfad
on error
beep
end try
return input
end run
on neuer_Pfad(lokaler_Pfad)
set {oAStID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "/"}
set lokaler_Pfad to text items of lokaler_Pfad
set AppleScript's text item delimiters to ("'/'")
set lokaler_Pfad to (lokaler_Pfad as string)
set AppleScript's text item delimiters to (oAStID)
set lokaler_Pfad to (((get characters 2 through ((count lokaler_Pfad)) of lokaler_Pfad) as string) & "'")
if (lokaler_Pfad ends with "/''") then
set lokaler_Pfad to (get characters 1 through ((count lokaler_Pfad) - 2) of lokaler_Pfad) as string
end if
return lokaler_Pfad
end neuer_Pfad
-----
Menu bar 'Script Menu' item '.scpt' code ...
on run
try
tell application "Finder" to set der_Auswahl to selection
if ((count der_Auswahl) = 1) then
set der_Auswahlpfad to my neuer_Pfad(POSIX path of (der_Auswahl as string))
else
set der_Auswahlpfad to {}
repeat with i in der_Auswahl
copy (my neuer_Pfad(POSIX path of (i as string))) to end of der_Auswahlpfad
end repeat
set {oAStID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, " "}
set der_Auswahlpfad to (der_Auswahlpfad as string)
set AppleScript's text item delimiters to (oAStID)
end if
set der_Pfad to my neuer_Pfad(POSIX path of (choose folder))
do shell script "mv " & der_Auswahlpfad & " " & der_Pfad
on error
beep
end try
end run
on neuer_Pfad(lokaler_Pfad)
set {oAStID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "/"}
set lokaler_Pfad to text items of lokaler_Pfad
set AppleScript's text item delimiters to ("'/'")
set lokaler_Pfad to (lokaler_Pfad as string)
set AppleScript's text item delimiters to (oAStID)
set lokaler_Pfad to (((get characters 2 through ((count lokaler_Pfad)) of lokaler_Pfad) as string) & "'")
if (lokaler_Pfad ends with "/''") then
set lokaler_Pfad to (get characters 1 through ((count lokaler_Pfad) - 2) of lokaler_Pfad) as string
end if
return lokaler_Pfad
end neuer_Pfad
magilum
May 1st, 2006, 10:00 PM
Cool stuff, barhar. It seems to work great. Could you elaborate on some of what's going on?
barhar
May 2nd, 2006, 01:32 AM
A selection of items is obtained by 'Finder".
If no items were selected 'try ... end try' captures the 'Finder' selection error, and a 'Beep' is made.
If a single item was selected, the item's path is handled as a string item.
If multiple items were selected, a single string of all the individual item's paths is created.
'mv ...' copies the source items to a destination, and then deletes the source items.
'neuer_Pfad()' surrounds each folder (directory) and / or file name in single quotes. This allows 'mv' to properly handle item names which include spaces.
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by
vBSEO 3.3.0 RC1