Applescript - File rename question

MemphisGriz

macrumors newbie
Original poster
Apr 15, 2016
4
0
Memphis, TN
I'm new to Applescript but I have a question.

Screen Shot 2016-04-15 at 11.52.39 AM.png

[doublepost=1460745401][/doublepost]I suspect there is a way to tell the Finder to search for the word "PMS".

From there I could tell it to rename the file
 

Mark FX

macrumors regular
Nov 18, 2011
119
8
West Sussex, UK
I've never used the Automator app, as I've always created Applescript apps in Script Editor or Xcode to achieve this kind of workflow, but I've posted a basic vanilla AppleScript below that works on the filename you posted, and it will only work on filenames with that very same filename format.

Code:
set theLenFileAlias to (choose file) as alias

tell application "Finder"
    set theLenFilePath to file theLenFileAlias as text
    set theLenFileNameText to name of theLenFileAlias as text
    set theNewLenFileName to my createNewLenFileNameFromText:theLenFileNameText
    -- return theNewLenFileName -- Uncomment this line to see the return value of createNewLenFileNameFromText: method.
    if theNewLenFileName ≠ missing value then
        set name of file theLenFilePath to theNewLenFileName -- Changes the name of the chosen file to the new name format.
    end if
end tell

on createNewLenFileNameFromText:theOldLenFileNameText
    set oldTextItemDelimiters to AppleScript's text item delimiters
    set AppleScript's text item delimiters to "."
    set theLenFileNameTextItemsList to text items of theOldLenFileNameText as list
    set AppleScript's text item delimiters to oldTextItemDelimiters
    -- return theLenFileNameTextItemsList -- Uncomment this line to see the file name text items.
    if (count of theLenFileNameTextItemsList) ≠ 6 then
        return missing value
    end if
    set theJobNumberText to (item 1 of theLenFileNameTextItemsList) as text
    -- return theJobNumberText -- Uncomment this line to see the job number text.
    try
        -- Change to item 4 if the first PMS number in the filename is wanted.
        set thePMSNumberWordsList to words of (item 5 of theLenFileNameTextItemsList) as list
        -- return thePMSNumberWordsList -- Uncomment this line to see the PMS number word items.
        set thePMSNumberText to (item 2 of thePMSNumberWordsList) as text
        -- return thePMSNumberText -- Uncomment this line to see the PMS number as text.
    on error
        return missing value
    end try
    set theNewLenFileName to (theJobNumberText & "-" & thePMSNumberText & ".len") as text
    return theNewLenFileName
end createNewLenFileNameFromText:
If you copy and paste this code into a new AppleScript file in Script Editor, and then hit the run button, you will be able to choose a file with your posted filename format, and it will rename it with the style you've asked for.

I've also commented out some method return values, that you can uncomment, to see how the script breaks the original filename, and then creates a new filename based on the broken down elements.
This script does not represent a complete solution to your problem, but may give you a basis on which to build upon.

Also the createNewLenFileNameFromText: method syntax will only work on OSX 10.10 or 10.11, so if your using an earlier version of OSX, then let us know, and we will show you how to change the method declaration to suit your operating system version.

And maybe another member with knowledge of the Automator app, may be able to show you how to change and incorporate it into an Automator workflow.

Regards mark
 
Last edited:
  • Like
Reactions: MemphisGriz

superscape

macrumors 6502a
Feb 12, 2008
919
214
East Riding of Yorkshire, UK
Or a cheeky bit of shell script might help you:

Code:
theFileName="0915-103284.Flat.Layout.1A.PMS 466.fsPMS 466 C.len"; thePMSNo=`echo "$theFileName" | grep -o "PMS.*" | cut -f2 -d" " | cut -f1 -d"."` ; theJobNo=`echo "$theFileName" | cut -f1 -d"."` ; echo "$theJobNo-$thePMSNo.len"
It assumes that the part of your file up until the first dot is the job number, and the bit from the first "PMS" through to the next space is your PMS No. Not sure whether that's valid for you or not.

I wrote a short piece on my blog a little while ago on mixing shell script with Automator that you might find useful if you take this approach:

https://www.ghostotter.com/making-shell-scripts-little-user-friendly/

Good luck!
 

MemphisGriz

macrumors newbie
Original poster
Apr 15, 2016
4
0
Memphis, TN
I've never used the Automator app, as I've always created Applescript apps in Script Editor or Xcode to achieve this kind of workflow, but I've posted a basic vanilla AppleScript below that works on the filename you posted, and it will only work on filenames with that very same filename format.

Code:
set theLenFileAlias to (choose file) as alias

tell application "Finder"
    set theLenFilePath to file theLenFileAlias as text
    set theLenFileNameText to name of theLenFileAlias as text
    set theNewLenFileName to my createNewLenFileNameFromText:theLenFileNameText
    -- return theNewLenFileName -- Uncomment this line to see the return value of createNewLenFileNameFromText: method.
    if theNewLenFileName ≠ missing value then
        set name of file theLenFilePath to theNewLenFileName -- Changes the name of the chosen file to the new name format.
    end if
end tell

on createNewLenFileNameFromText:theOldLenFileNameText
    set oldTextItemDelimiters to AppleScript's text item delimiters
    set AppleScript's text item delimiters to "."
    set theLenFileNameTextItemsList to text items of theOldLenFileNameText as list
    set AppleScript's text item delimiters to oldTextItemDelimiters
    -- return theLenFileNameTextItemsList -- Uncomment this line to see the file name text items.
    if (count of theLenFileNameTextItemsList) ≠ 6 then
        return missing value
    end if
    set theJobNumberText to (item 1 of theLenFileNameTextItemsList) as text
    -- return theJobNumberText -- Uncomment this line to see the job number text.
    try
        -- Change to item 4 if the first PMS number in the filename is wanted.
        set thePMSNumberWordsList to words of (item 5 of theLenFileNameTextItemsList) as list
        -- return thePMSNumberWordsList -- Uncomment this line to see the PMS number word items.
        set thePMSNumberText to (item 2 of thePMSNumberWordsList) as text
        -- return thePMSNumberText -- Uncomment this line to see the PMS number as text.
    on error
        return missing value
    end try
    set theNewLenFileName to (theJobNumberText & "-" & thePMSNumberText & ".len") as text
    return theNewLenFileName
end createNewLenFileNameFromText:
If you copy and paste this code into a new AppleScript file in Script Editor, and then hit the run button, you will be able to choose a file with your posted filename format, and it will rename it with the style you've asked for.

I've also commented out some method return values, that you can uncomment, to see how the script breaks the original filename, and then creates a new filename based on the broken down elements.
This script does not represent a complete solution to your problem, but may give you a basis on which to build upon.

Also the createNewLenFileNameFromText: method syntax will only work on OSX 10.10 or 10.11, so if your using an earlier version of OSX, then let us know, and we will show you how to change the method declaration to suit your operating system version.

And maybe another member with knowledge of the Automator app, may be able to show you how to change and incorporate it into an Automator workflow.

Regards mark
what if I want to be able to select more than one file at a time?

If I start out the script with "set theLenFileAlias to (choose file with prompt "Please select all PMS Len files:" with multiple selections allowed) as alias"

I get an error now and the script fails
 

Mark FX

macrumors regular
Nov 18, 2011
119
8
West Sussex, UK
When you select more than one file, you do not get a single file alias, you get an AppleScript list of file alias's.
An AppleScript list is like an Array in other programming languages.
So change the "choose file" command to return a list like the new example I've posted below.

Once you have a list of file alias's, you then need to loop through the items in the list and process each one individually.
You do this in AppleScript with a "Repeat loop", if you do not understand repeat loop syntax, then I strongly advise you learn the basics of the AppleScript language, as there are several ways to implement a repeat loop, and I've only shown you one way in the example below.

Code:
try
    set theLenFileAliasList to (choose file with prompt "Please select all PMS Len files:" with multiple selections allowed) as list
on error
    -- The user clicked the cancel button in the choose file dialog box.
    return "The User Cancelled"
end try

-- return count of theLenFileAliasList -- Uncomment this line to see the number of chosen files.
-- return theLenFileAliasList -- Uncomment this line to see the list of chosen files.

tell application "Finder"
    repeat with theLenFileAlias in theLenFileAliasList
        set theLenFilePath to file theLenFileAlias as text
        set theLenFileNameText to name of theLenFileAlias as text
        set theNewLenFileName to (my createNewLenFileNameFromText:theLenFileNameText)
        -- return theNewLenFileName -- Uncomment this line to see the return value of createNewLenFileNameFromText: method.
        if theNewLenFileName ≠ missing value then
            set name of file theLenFilePath to theNewLenFileName -- Changes the name of the chosen file to the new name format.
        else
            -- The file renaming process failed.
            return "File renaming process failed"
        end if
        -- delay (0.5) -- You may need a delay if your processing hundreds of files, but not for a few.
    end repeat
end tell

on createNewLenFileNameFromText:theOldLenFileNameText
    set oldTextItemDelimiters to AppleScript's text item delimiters
    set AppleScript's text item delimiters to "."
    set theLenFileNameTextItemsList to text items of theOldLenFileNameText as list
    set AppleScript's text item delimiters to oldTextItemDelimiters
    -- return theLenFileNameTextItemsList -- Uncomment this line to see the file name text items.
    if (count of theLenFileNameTextItemsList) ≠ 6 then
        return missing value
    end if
    set theJobNumberText to (item 1 of theLenFileNameTextItemsList) as text
    -- return theJobNumberText -- Uncomment this line to see the job number text.
    try
        -- Change to item 4 if the first PMS number in the filename is wanted.
        set thePMSNumberWordsList to words of (item 5 of theLenFileNameTextItemsList) as list
        -- return thePMSNumberWordsList -- Uncomment this line to see the PMS number word items.
        set thePMSNumberText to (item 2 of thePMSNumberWordsList) as text
        -- return thePMSNumberText -- Uncomment this line to see the PMS number as text.
    on error
        return missing value
    end try
    set theNewLenFileName to (theJobNumberText & "-" & thePMSNumberText & ".len") as text
    return theNewLenFileName
end createNewLenFileNameFromText:
I have to reiterate my previous advice, that this example code is not an ideal example of how you would create a fully fledged AppleScript App, but merely the file renaming part based on the files having the exact naming format you posted.

If your AppleScript App needs to grow and become more advanced, I would recommend you learn the basics of the language, otherwise your simply going to change posted code and run into the next error, without you understanding how the errors are occurring.

Hope this helps.

Regards Mark
 
Last edited: