Hello All,
I’m new to Applescript and to AppleScript.net but I can’t get this seemingly simple Folder Action to work. I want to change the name of a pdf file dropped in a hot folder to change the name to After.pdf. This is assuming that only one file will be dropped and the file is already a pdf. I tried writing this code from samples I’ve read but it just doesn’t work. Can anyone help me?
Here is my code:
on adding folder items to this_folder after receiving dropped_items
tell application "Finder"
set the_dropped_file_name of dropped_items to "After.pdf"
end tell
end adding folder items to
dropped_items is a list, e.g. {alias “Disk:Users:user:Desktop:item.ext”}, even if it’s just one item. Since the Finder can’t rename a list, it will error. The solution is to change the line to handle item 1 of dropped_items. Also, the_dropped_file_name variable isn’t appropriate. The Finder accesses the name of a Finder item through the name property.
tell application "Finder" to set name of item 1 of dropped_items to "After.pdf"
Also of note: after renaming the item, the item is counted as added to the folder, which will trigger the Folder Action once again. I’ve got to go right now, but I’ll look for a script I’ve got floating around where I tried to work around this.
In addition to JB’s excellent point, a few other points:
You can name “this_folder” and “dropped_items” anything you like - they are just variables holding the data.
When there’s a possibility of errors, it’s a good idea to put the error-prone stuff in a “try” block like this:
on adding folder items to this_folder after receiving dropped_items
try
tell application "Finder" to set name of item 1 of dropped_items to "After.pdf"
on error theError -- "theError" can be whatever you choose to call it
display dialog theError
end try
end adding folder items to
If you had done that to your script (with the name part changed but without the “item 1”), the error message would have told you that there was a problem with trying to get the name of a what it saw as a list.
I didn’t understand about a file name being a list, but after you explained that fact Joseph, I researched in Hanaan Rosenthal’s book “Applescript”, witch I am currently reading. On page 600 he wrote a similar script changing a file directory.
So this script worked like a charm and even what Adam wrote. I will try them both out in my work flow.
on adding folder items to this_folder after receiving dropped_items
set the_dropped_file_path to item 1 of dropped_items
tell application "Finder"
set name of item 1 of dropped_items to "After.pdf"
end tell
end adding folder items to
Thanks Again for your help and replying so quickly!
Aaron
I was working with our new script but I keep getting a duplicate file error message when I deleted the After.pdf in the hot folder and drop a new file on it. I tried to add the After.pdf back into the folder but it says that I cannot replaced After.pdf because it is invisible. So I tried adding to the script to delete the current After.pdf in the folder and then change the new file to After.pdf. I was also thinking this is what you where saying earlier that there are two actions happening one when I drop the file and one when it changes then name.
on adding folder items to this_folder after receiving dropped_items
tell application "Finder"
delete file "After.pdf" in this_folder
try
tell application "Finder" to set name of item 1 of dropped_items to "After.pdf"
on error theError -- "theError" can be whatever you choose to call it
display dialog theError
end try
end tell
end adding folder items to
Can you help again with the invisible file?
Thanks,
Aaron
This is a difficulty with changing the name of a file in a folder action. In his post in this thread, Joseph Briggs said
“Also of note: after renaming the item, the item is counted as added to the folder, which will trigger the Folder Action once again. I’ve got to go right now, but I’ll look for a script I’ve got floating around where I tried to work around this.”
The folder action interprets the new file name as a new item added to the folder and tries to do the script again, failing because there’s already a file by that name: the one you just dropped.
Since it’s not clear why you want to do this, I can’t suggest a way around it. You can’t just delete the original because the folder action still creates the “new” one and then tries to do it again.
A better approach might be to create a droplet script that would change the name of files dropped on it and then move them to your folder with replacing (which will lose whatever was there before - this folder can only hold one file).
Tell us what you want to do - what you’re trying to do doesn’t make much sense.
Hi Adam,
Thanks for replying.
I am trying to set up a hot folder that will rename a pdf file to After.pdf until a new pdf file is dropped in. The reason for this is is having an Indesign document with two layers witch have a link on each layer to the Before.pdf and After.pdf, so I can automatically updated the document when I drop a new pdf in the hot folders.
I’m learning as I go and I thought this would be relatively simple. Any help and suggestions would be greatly appreciated.
Sorry for not getting back to this; life has been crazy: upgrading Macs and servers, dogs with cancer, all kinds of madness. Anyway.
I had been trying to find a workaround to the renaming thing and I wasn’t sure if I had found it yet. And it doesn’t look like I have. I tried adding a property changedNames and checking to see if the added item’s name is in the list and if not to rename it and add the name to a list of names that replace the changedNames list after all dropped items are processed. This didn’t work every time, especially with multiple files. I tried a few other things but nothing consistently works. Sorry.
Let me join Joseph Briggs, here, Aaron: I can’t think of a way around this basic behavior of folder actions either. What I would do in your place is to create a folder to hold these files (the Before & After PDFs) and then create a droplet script that renamed the file dropped on it and then moved it to your designated folder “with replacing”, i.e. replacing any that were there before. Unless you want to include a dialog to lets you choose whether the file is “before” or “after” you’ll need a separate droplet for each.
What makes a script application (i.e., a script saved as an application) a droplet is an on open handler in the script. The act of dropping a Finder item on a droplet sends an open message to the droplet. Your application must have an on open handler that traps the open message and gets the script going.
on open dropped_items
set fileName to button returned of (display dialog "Is this a Before or After file?" buttons {"Before", "After"} default button "After")
tell application "Finder"
try
set name of item 1 of dropped_items to fileName & ".pdf"
move item 1 of dropped_items to (choose folder) with replacing -- in place of choose folder you can do that in a separate script and copy the result here in place of (choose folder) so you won't have to choose every time.
on error theError
display dialog theError
end try
end tell
end open
[Joseph: sorry about the dog - that’s probably more worrisome than the servers, etc. (aka your job) – acb]
Thanks to both of you for replying. I tried your script Adam and it worked great!
But I am still working on the Folder Action. I did set up the script without the Ask folder option you suggested and placed the droplets in one folder on my desktop, so I have two droplets in one folder:
on open dropped_items
tell application "Finder"
set name of item 1 of dropped_items to "Before.pdf"
move item 1 of dropped_items to folder "Mac 12:Users:Mac:Desktop:QA" with replacing
end tell
end open
on open dropped_items
tell application "Finder"
set name of item 1 of dropped_items to "After.pdf"
move item 1 of dropped_items to folder "Mac 12:Users:Mac:Desktop:QA" with replacing
end tell
end open