Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

alentzu

macrumors newbie
Original poster
Dec 23, 2018
7
0
Switzerland
Hello

I am a complete beginner with AppleScript and just spent some hours to learn AppleScript - but I failed... I would be very thankful for someone’s help. I‘d like to automate 2 processes:


When I drop a file on an "AppleScript App"/Droplet: Rename that file with the parent folder in front and current date at the end:


FolderA/Scan1.pdf

>> FolderA/FolderA Scan1 2018-12-23.pdf



And a second automation:

When I drop a file on an AppleScript App: Show me a list of 5 names that I can select from. Then rename that file with the name selected and current date at the end.


Scan1.pdf

>> show me a dialog to select from: "Cat" OR "Dog" OR "Mouse" ...

>> then rename to: Mouse 2018-12-23.pdf



Thank you so much for any help!

Kind regards, Alen
 

kryten2

macrumors 65816
Mar 17, 2012
1,114
99
Belgium
Automator :
  • Create application with the following actions :
  • Set Value of Variable : ex: Storage
  • Run AppleScript : replace code with:
Code:
on run {input, parameters}
    (* Your script goes here *)
    tell application "System Events"
        set fileContainer to get name of container of first item of input
    end tell
    return fileContainer
    --return input
end run
  • Set Value of Variable : ex: fileContainer
  • Get Value of Variable : ex: Storage : Ignore this actions's input
  • Rename Finder Items: Add Date or Time
  • Rename Finder Items: Add Text : ex: Add: fileContainer before name
Drag the fileContainer variable into the Add: field
Applescript:

Code:
on run
    set theFiles to choose file with multiple selections allowed
    open theFiles
end run
on open theFiles
    -- get current date
    set theDateStamp to do shell script "date +%Y-%m-%d"
    -- loop thru files
    repeat with aFile in theFiles
        -- get parent folder, parent folder name, extension
        tell application "System Events" to set {fileContainer, fileContainerName, fileNameExt} to aFile's {container, name of container, name extension}
        -- get basename
        set fileName to do shell script "basename -s ." & fileNameExt & space & quoted form of POSIX path of aFile
        -- construct new file name
        set newFileName to fileContainerName & space & fileName & space & theDateStamp & "." & fileNameExt
        -- rename file
        tell application "System Events"
            if (not (exists file newFileName of fileContainer)) then
                set name of aFile to newFileName
            end if
        end tell
    end repeat
end open

Applescript :

Code:
on run
    set theFiles to choose file with multiple selections allowed
    open theFiles
end run
on open theFiles
    set theDateStamp to do shell script "date +%Y-%m-%d"
    repeat with aFile in theFiles
        set listResult to choose from list {"bar", "car", "deer", "cat", "dog"}
        if listResult is not false then
            set fileName to item 1 of the listResult
            tell application "System Events" to set {fileContainer, fileContainerName, fileNameExt} to aFile's {container, name of container, name extension}
            set newFileName to fileContainerName & space & fileName & space & theDateStamp & "." & fileNameExt
            tell application "System Events"
                if (not (exists file newFileName of fileContainer)) then
                    set name of aFile to newFileName
                end if
            end tell
        end if
    end repeat
end open
 
Last edited:
  • Like
Reactions: alentzu

alentzu

macrumors newbie
Original poster
Dec 23, 2018
7
0
Switzerland
Automator :
  • Create application with the following actions :
  • Set Value of Variable : ex: Storage
  • Run AppleScript : replace code with:
Code:
on run {input, parameters}
    (* Your script goes here *)
    tell application "System Events"
        set fileContainer to get name of container of first item of input
    end tell
    return fileContainer
    --return input
end run
  • Set Value of Variable : ex: fileContainer
  • Get Value of Variable : ex: Storage : Ignore this actions's input
  • Rename Finder Items: Add Date or Time
  • Rename Finder Items: Add Text : ex: Add: fileContainer before name
Drag the fileContainer variable into the Add: field
Applescript:

Code:
on run
    set theFiles to choose file with multiple selections allowed
    open theFiles
end run
on open theFiles
    -- get current date
    set dateStamp to do shell script "date +%Y-%m-%d"
    -- loop thru files
    repeat with aFile in theFiles
        -- get parent folder, parent folder name, extension
        tell application "System Events" to set {fileContainer, fileContainerName, fileNameExt} to aFile's {container, name of container, name extension}
        -- get basename
        set fileName to do shell script "basename -s ." & fileNameExt & space & quoted form of POSIX path of aFile
        -- construct new file name
        set newFileName to fileContainerName & space & fileName & space & theDateStamp & "." & fileNameExt
        -- rename file
        tell application "System Events"
            if (not (exists file newFileName of fileContainer)) then
                set name of aFile to newFileName
            end if
        end tell
    end repeat
end open

Applescript :

Code:
on run
    set theFiles to choose file with multiple selections allowed
    open theFiles
end run
on open theFiles
    set theDateStamp to do shell script "date +%Y-%m-%d"
    repeat with aFile in theFiles
        set listResult to choose from list {"bar", "car", "deer", "cat", "dog"}
        if listResult is not false then
            set fileName to item 1 of the listResult
            tell application "System Events" to set {fileContainer, fileContainerName, fileNameExt} to aFile's {container, name of container, name extension}
            set newFileName to fileContainerName & space & fileName & space & theDateStamp & "." & fileNameExt
            tell application "System Events"
                if (not (exists file newFileName of fileContainer)) then
                    set name of aFile to newFileName
                end if
            end tell
        end if
    end repeat
end open
[doublepost=1545930836][/doublepost]Thank you very much! The first part worked immediately. I struggle with the second one, but I keep playing around...

It really helps!
 

alentzu

macrumors newbie
Original poster
Dec 23, 2018
7
0
Switzerland
I got the error message:
The Variable „theDateStamp“ is not defined. (-2753)

And I don't know why it works in the second script though. I'll play around to figure out.
 

kryten2

macrumors 65816
Mar 17, 2012
1,114
99
Belgium
I got the error message:
The Variable „theDateStamp“ is not defined. (-2753)

And I don't know why it works in the second script though. I'll play around to figure out.
Change this :
Code:
    -- get current date
    set dateStamp to do shell script "date +%Y-%m-%d"
to this in the first script :
Code:
    -- get current date
    set theDateStamp to do shell script "date +%Y-%m-%d"
Sorry I didn't see that one.
 
  • Like
Reactions: alentzu

alentzu

macrumors newbie
Original poster
Dec 23, 2018
7
0
Switzerland
Could you help me with one more thing?

Rather than have a droplet, I'd like to run these 2 scripts with a shortcut for the selected file. I googled and found hints that I should make an Automator action and then assign a shortcut. Where I fail is the code in AppleScript in Automator: How should I adapt your code to execute it through an Automator action?
I get error messages, and I understand that the code should not begin with "on run"... but how then?

And where do these code lines belog exactly to apply a color tag?
property labelColor : {none:0, orange:1, red:2, yellow:3, blue:4, purple:5, green:6, gray:7}
set label index of aFile to orange of labelColor
 

kryten2

macrumors 65816
Mar 17, 2012
1,114
99
Belgium
I'm sorry that things seem to be confusing. The Applescript examples code is to be used for creating a dropplet. The on run handler is used to test the droplet from within ScriptEditor and to make a droplet respond when it is double clicked. It's not really needed and can throw an error as you noticed when you start pasting stuff.
Just create an Automator Service from the first Automator example in my first reply:
  • Service receives selected files or folders in Finder
  • The six actions I gave in my first reply
  • Label Finder Items
To apply a color tag from the droplet applescript code :

Code:
     -- rename file
       tell application "System Events"
           if (not (exists file newFileName of fileContainer)) then
               set name of aFile to newFileName
           end if
       end tell
    -- apply color tag
      tell application "Finder" to set label index of aFile to 1

Automator Service for the second droplet :
  • Service receives selected files or folders in Finder
  • Set Value of Variable : ex: Storage
  • Run AppleScript : replace code with: Ignore this actions's input!
Code:
set listResult to choose from list {"bar","dog","cat"}
if the listResult is not false then
       set fileName to item 1 of the listResult
end if
return fileName
  • Set Value of Variable : ex: fileName
  • Get Value of Variable : ex: Storage : Ignore this actions's input
  • Rename Finder Items: Name Single Item : ex: Name: Basename only to fileName
Drag the fileName variable into the to: field
  • Rename Finder Items: Add Date or Time
  • Label Finder Items
 
Last edited:
  • Like
Reactions: alentzu
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.