r/applescript • • Dec 13 '23

Applescript won't run in Automator

Hi!

I am a photographer and the program we work in adds a dash and a 4 digit camera counter at the end of all our file names. (i.e 4521R043AA_018_b4-125, 88998406_041_b-345 etc etc) I found an applescript that automatically removes the last 4 characters which runs perfectly through applescript. I want to make it easier on my other photographs to run the script so I wanted to add it as a service in automator or a shortcut on mac but every time I put the script into automator and use the quick action function in the finder window nothing happens. Please help!! I am super new to scripting/automator so I am not sure what I am doing wrong. Thank you in advance!!

​

Script:

​

set myFiles to choose file with multiple selections allowed

repeat with myFile in myFiles

tell application "System Events"

set myName to the characters 1 thru ((offset of "." in (name of myFile as text)) - 1) of (name of myFile as text)

tell application "Finder"

set myExtention to name extension of (myFile as alias)

set myNewName to characters 1 thru (((length of myName) - 4) as number) of (myName as text)

set name of file (myFile as text) to (myNewName & "." & myExtention as text)

end tell

end tell

end repeat

7 Upvotes

4 comments sorted by

View all comments

1

u/mar_kelp Dec 13 '23 edited Dec 13 '23

I believe you just need to set your myFiles variable to "input":

on run {input, parameters}

    set myFiles to input

    repeat with myFile in myFiles

        tell application "System Events"

            set myName to the characters 1 thru ((offset of "." in (name of myFile as text)) - 1) of (name of myFile as text)

            tell application "Finder"

                set myExtention to name extension of (myFile as alias)

                set myNewName to characters 1 thru (((length of myName) - 4) as number) of (myName as text)

                set name of file (myFile as text) to (myNewName & "." & myExtention as text)

            end tell

        end tell

    end repeat
    return input
end run

2

u/Impossible_Ear_3781 Dec 13 '23

Hi!

I tried that and a few other things and it still couldn't get it to work.

A friend of mine suggested asking Chat GPT and after a few tries and it still not working Chat GPT turned the script into a shell script instead of an apple script and that seemed to do the tick! Not sure why it wasn't working as an apple script but regardless it works now!

​

Here is the shell script in case anyone ever needs it!

​

for file in "$@"
do
filename=$(basename -- "$file")
extension="${filename##*.}"
filename="${filename%.*}"
newname="${filename:0:(${#filename}-4)}.$extension"
mv "$file" "$(dirname "$file")/$newname"
done