Issue with renaming file with applescript

This seems very silly to ask, but I am VERY new to apple scripts and trying to learn.


I get an error with the following script. I am setting it up in automator to take a file dropped into a folder with folder actions set up. But I keep getting a "Unspecified error"


It should take the file and rename it to the contents of the text in the clipboard and preserve the extension.


tell application "Finder" to activate
tell application "Finder"
   set theExt to name extension of theFile
   set the clipboard to «class ktxt» of ((the clipboard as text) as record) # remove styles from the clipboard
   set theNewname to the clipboard
   set name of theFile to theNewname
   set name extension of theFile to theExt
end tell


Any help would be appreciated

Mac OS X (10.5)

Posted on Jun 2, 2016 10:55 AM

Reply
2 replies

Jun 3, 2016 3:59 AM in response to datagears

You don't need to activate Finder unless you want it to come to the foreground and, as you are already dropping a file on a folder, I suspect it's already there!


I don't really understand your workflow to understand what you are putting into the clipboard to start with, so there might need to be a tweak if this script doesn't work for you. Whilst you can set the name extension explicitly, I have just built the new name including the extension into the variable newName.


NOTE: I have put a test in to prevent the name change going ahead. If you don't want this, delete where indicated. As always, make sure you have a backup!


tell application "Finder"

set theExtension to name extension of theFile

set oldName to name of theFile

set newName to (the clipboard) & "." & theExtension

display dialog "Are you sure you want to rename " & oldName & " to " & newName buttons {"Yes", "No"} --remove this line and two others below if you don't want to check

if button returned of result is "Yes" then --remove

set name of theFile to newName

end if --remove

end tell

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Issue with renaming file with applescript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.