Jump to content

Applesript for file name changes


Recommended Posts

<p>I have had a Mac computer with Mac OS for many years. Over this time rules for file naming have changed. And the original pict files/quickdraw are no longer recognized by Photoshop. I had to use an older version of Photoshop to open and then save the pict files in jpg. If you are familiar with the Automate/Batch action you can change a folder of files in a hurry.<br>

Meanwhile my woe was that there were many thousands of files that had accumulated with wrong naming characters. Additionally I had many files that had not been saved with ".jpg" extensions. The files still work but Lightroom will not play with them.<br>

Specifically I had used a file name convention naming roll number and frame number: 2406/03. The problem was the "/" is not allowed and so the file was ignored by Lightroom.<br>

The Internet is great and powerful! Two searches later have led me to find the scripts that will change my file names. After you write the script into Applescript editor you will save: Library/Scripts/Apple/Folder Action Scripts.<br>

Then create a new folder. Control-click and choose Folder Action Setup. Add your files that need changes and the names will be changed saving untold time.<br>

Here are two helpful scripts to help you rename your files. The rename script has " " I chose this as a space/no character. For windows naming you would need "_" an underscore.<br>

I apologize that I cannot return to find the webpage that gave me the scripts but thanks to the kind folks who posted and saved me so much time.</p>

<p>To add ".jpg" Applescript:</p>

<p>on adding folder items to myFolder after receiving myFiles<br /> repeat with myFile in myFiles<br /> set myPath to (POSIX path of myFile)<br /> set myType to do shell script "file --mime-type -br " & (quoted form of myPath)<br /> if myType is "image/jpeg" and myPath does not end with ".jpg" and myPath does not end with ".jpeg" then<br /> set newExtension to ".jpg"<br /> else if myType is "image/gif" and myPath does not end with ".gif" then<br /> set newExtension to ".gif"<br /> else if myType is "image/png" and myPath does not end with ".png" then<br /> set newExtension to ".png"<br /> else<br /> set newExtension to null<br /> end if<br /> tell application "Finder"<br /> if newExtension is not null then<br /> set name of myFile to (name of myFile) & newExtension<br /> end if<br /> end tell<br /> end repeat<br />end adding folder items to</p>

<p>To rename and replace characters Applescript:</p>

<p>--set the list of characters you want to replace<br />--disallowedChars will be replaced with the replacementChar<br />--in this case, an underscore<br />property disallowedChars : ":;,/|!@#$%^&*()+"<br /><br />--anything in disallowedChars2 will be removed altogether<br />property disallowedChars2 : "'"<br /><br />--set the character you'd like to use to replace the invalid <br />--characters specified in disallowedChars<br />property replacementCharacter : " "<br /><br />on adding folder items to this_folder after receiving added_items<br /> <br /> tell application "Finder"<br /> try<br /> repeat with x in added_items<br /> <br /> set fileNamed to name of x<br /> set newName to my CleanName(fileNamed)<br /> set (name of x) to newName<br /> <br /> end repeat<br /> on error<br /> display dialog "An error occurred."<br /> end try<br /> end tell<br /> <br />end adding folder items to<br /><br />--function for cleaning the characters from the file name<br />on CleanName(theName)<br /> <br /> set newName to ""<br /> repeat with i from 1 to length of theName<br /> <br /> --check if the character is in disallowedChars<br /> --replace it with the replacementCharacter if it is<br /> if ((character i of theName) is in disallowedChars) then<br /> set newName to newName & replacementCharacter<br /> <br /> --check if the character is in disallowedChars2<br /> --remove it completely if it is<br /> else if ((character i of theName) is in disallowedChars2) then<br /> set newName to newName & ""<br /> <br /> --if the character is not in either disallowedChars or<br /> --disallowedChars2, keep it in the file name<br /> else<br /> set newName to newName & character i of theName<br /> <br /> end if<br /> end repeat<br /> <br /> return newName<br />end CleanName</p>

Link to comment
Share on other sites

<p>That's impressive, but there are other solutions.<br /> First, if you have Adobe Bridge, you can do easy changes of names, suffixes, and so on using the "<strong>batch rename</strong>" under the <strong>tools</strong> menu. Select the files in the Bridge display and explore the various pull-down options for replacement, adding, changing, etc. You can see what the file thinks it belongs to in the Metadata part of the window.</p>

<p>An alternative that I also highly recommend is to keep a copy of GraphicConverter handy. You can drag and drop most files on the program icon and it will allow read, rename, conversion, etc. It really shines in converting many alien (non-Mac) and ancient image formats to more modern/Macish ones. It's modestly priced at http://www.lemkesoft.de/en/image-editing-slideshow-browser-batch-conversion-metadata-and-more-on-your-mac/</p>

Link to comment
Share on other sites

<p>Having been changing old files by various methods, I finally realized how very useful Bridge was in this "<strong><em>Tools</em></strong>" menu area.</p>

<p>I also had a large, actually very large, collection of older files without suffixes from the Macintosh from 1984 and before OS X. I always considered the use of suffixes as a less elegant solution than the old System 9 and earlier "resource fork" in which the file itself recorded its "parent" application. </p>

<p>A lot of these are identified as .exe files by the newer, dumber (in this one sense, at least), OS.<br>

A lot of un-suffixed files also will open Powerpoint as a sort of default "momma" if you double click on them. :(</p>

<p>I know that the suffixes can be hidden, but as long as they're there, I think it best to just surrender to this retrograde movement.</p>

Link to comment
Share on other sites

<p>Well, my hope was to help someone else avoid the pain of changing thousands of file names manually one at a time. So this info is out there for the Photo.net community to use.<br>

I hadn't opened my pict file folders for a long time. I finally got around to organizing old pictures on my back up disks. Both leopard and lion OS X go crazy when you open a folder that contains only pict files. The later versions of Photoshop do not recognize pict files anymore. My work around, I had to go back and use an early version of Photoshop to convert pict to jpg. Yeah, old files can sneak up and bite you as the technology changes.</p>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...