View Full Version : Moving files using an Applescript Droplet?
jacktabby
05-03-2010, 06:35 AM
I'm trying to move files dropped into an applescript droplet into a folder.
The Folder is: /Users/Jack/.Trash
So here is my applescript so far:
on open the_Droppings
display dialog "Are You Sure You Want Move The Dropped Item(s) to the Trash?" buttons {"Yes", "No"}
if the button returned of the result is "Yes" then
else
quit application
end if
end open
What I want to know is what do I type after:
if the button returned of the result is "Yes" then
Could I type something like:
tell application "Finder"
move dropped files to "/Users/Jack/.Trash"
end tell
Sorry. Newbie to applescript. Please Help Me.
P.S. Please don't tell me the command to delete to the trash. I want to do it like this for a reason. Thank you. :)
renaultssoftware
05-03-2010, 03:14 PM
on open names
display dialog "Are You Sure You Want Move The Dropped Item(s) to the Trash?" buttons {"Yes", "No"}
if the button returned of the result is "Yes" then
tell application "Finder"
delete names
end tell
end if
end open
Don't worry!! "delete names" WILL NOT delete the files permanently, it will just move them to the Trash. Look it up in Finder.sdef if you want proof.
Red_Menace
05-03-2010, 03:25 PM
The Finder only understands HFS paths (colon delimited), not POSIX paths (slash delimited). Normally you can use POSIX file to refer to a POSIX path, but there is a reason for using the standard command to move to the trash - the Finder (or System Events) don't really handle those 'dot' file names.
You can use a shell script to do it your way, though (note that there will be an error if the item already exists in the trash). To use the shell script, you will need to convert the list of dropped items into POSIX paths, so the statements would be:
on open droppedItems
set trashPath to "/Users/Jack/.Trash"
-- set trashPath to POSIX path of (path to trash) -- preferred way to get the path
display dialog "Are You Sure You Want Move The Dropped Item(s) to the Trash?" buttons {"Yes", "No"}
if the button returned of the result is "Yes" then
repeat with anItem in droppedItems -- convert list items and move to trash
set anItem to quoted form of POSIX path of anItem
do shell script "mv " & anItem & space & trashPath
end repeat
else
quit application
end if
end open
jacktabby
05-04-2010, 02:39 AM
Thank you Red_Menace!
I Have one more question. If there is a file with the same name as another in the trash can I make it rename that file to something like:
filename_2
Thank you for your help so far!
Red_Menace
05-04-2010, 05:52 PM
The following script adds a handler to get a unique name before moving the item to the trash. Note that the Finder (and System Events) can look into the trash, the move command just seems to have problems using the dot folder for the destination.
on run -- double-clicked or run from the Script Editor
open (choose file with multiple selections allowed)
end run
on open droppedItems -- items dropped
set trashPath to "/Users/jack/.Trash"
-- set trashPath to POSIX path of (path to trash) -- preferred way to get the path
display dialog "Are You Sure You Want To Move The Dropped Item(s) to the Trash?" buttons {"Yes", "No"}
if the button returned of the result is "Yes" then
repeat with anItem in droppedItems -- convert list items and move to trash
tell application "Finder" -- add suffix if the file already exists in the trash
set theName to name of anItem
set newName to my (getUniqueName for theName from trashPath)
if theName is not equal to newName then set name of anItem to newName
end tell
set anItem to quoted form of POSIX path of anItem -- anItem still refers to the renamed item
do shell script "mv " & anItem & space & trashPath
end repeat
else
quit application
end if
end open
to getUniqueName for someName from someFolder
(*
check if someName exists in someFolder, creating a new unique name if needed
parameters - someName [text]: a name.extension to check for
someFolder [mixed]: a folder to check
returns [text]: a unique name
*)
set {counter, divider} to {"00", "_"}
set here to -(offset of "." in ((reverse of text items of someName) as text)) - 1 -- hopefully none in the name
set theName to text 1 thru here of someName
if here is -1 then -- no extension
set theExtension to ""
else
set theExtension to text (here + 1) thru -1 of someName
end if
set newName to theName & theExtension
tell application "System Events" to tell (get name of items of folder (someFolder as text))
repeat while it contains newName
set counter to text 2 thru -1 of ((100 + counter + 1) as text) -- leading zeros
set newName to theName & divider & counter & theExtension
end repeat
end tell
return newName
end getUniqueName
jacktabby
05-05-2010, 06:54 AM
Thank you!
Just in case you're wondering why I wanted to make this script is because I wanted to put it in the finders toolbar so when I delete a file from an External drive it goes to my Mac's trash and is stored on my hard drive instead of the external drive.
Thanks again... Jacktabby :)
Red_Menace
05-05-2010, 08:00 AM
Note that for a locally connected external drive (one connected to your machine), there is a .Trashes directory containing folders for each user that is integrated into the overall 'Trash".
vBulletin® v3.8.7, Copyright ©2000-2013, vBulletin Solutions, Inc.