Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How do I run a shell script via AppleScript?

Seems obvious, "do shell script" but that doesn't work for me.


I have an Automator app which runs a shell script, I'd like to take that into an AppleScript. My AS doesn't run the shell script. Perhaps "do shell script' is expecting the script to be located elsewhere? The rest of my AS runs fine, but the shell script doesn't.


repeat with i from 1 to count of items in exportFolder


do shell script


"exiftool -overwrite_original -Photoshop:CopyrightFlag='True'"



done

end repeat


As I say this works within an Automator app, what changes do I need to make it work in AppleScript?


Thanks!

Posted on Apr 22, 2011 12:38 AM

Reply
Question marked as Best reply

Posted on Apr 22, 2011 4:25 AM

Here is an example AppleScript (downloaded from the samples on the ExifTool home page) that may help:


- Phil


on openthe_files

set exiftool_path to "/usr/bin/"

set exiftool_args to "-S"

repeat with the_file in the_files

set exiftool_args to exiftool_args & " " & quote & POSIX path of the_file & quote

end repeat

set the_md to do shell scriptexiftool_path & "exiftool " & exiftool_args

display dialog "ExifTool Metadata:" & return & return & the_md

end open


on run

set chosen_file to choose file with multiple selections allowed


openchosen_file

end run

15 replies
Question marked as Best reply

Apr 22, 2011 4:25 AM in response to David Gordon

Here is an example AppleScript (downloaded from the samples on the ExifTool home page) that may help:


- Phil


on openthe_files

set exiftool_path to "/usr/bin/"

set exiftool_args to "-S"

repeat with the_file in the_files

set exiftool_args to exiftool_args & " " & quote & POSIX path of the_file & quote

end repeat

set the_md to do shell scriptexiftool_path & "exiftool " & exiftool_args

display dialog "ExifTool Metadata:" & return & return & the_md

end open


on run

set chosen_file to choose file with multiple selections allowed


openchosen_file

end run

Apr 22, 2011 6:07 AM in response to David Gordon

If you were to indicate what happens when you run the script, what messages, what errors you're getting it would make trying to figure out whats going on a lot easier.


Looking at your exiftool shell command line I don't see where you're giving a filename to exiftool. I'm not overly familiar wiht exiftool butit oes need a file to work on, no?

Apr 22, 2011 8:05 AM in response to David Gordon

Your code does not look right. (i.e. why do you have a variable named 'done').

Also, I don't see where you're executing the script on a file (I added & i )

Give this a try:


repeat with i from 1 to count of items in exportFolder

do shell script "exiftool -overwrite_original -Photoshop:CopyrightFlag='True'" & space & quoted form of POSIX path of i

end repeat


Tony

Apr 22, 2011 7:57 AM in response to Frank Caggiano

I have not yet found a good way to post code.


In the code above, I used 'Table' with 1 col and 1 row.


With all the work in these new forums, I can't believe that Apple has taken away the {code} block and has not given us anything in it's place. Many older forums have a code button or accept [code] [/code]


....and worse, they broke the formatting on the old posts.


Tony

Apr 22, 2011 8:07 AM in response to David Gordon

Let's see, where to start... 🙂


repeat with i from 1 to count of items in exportFolder

What is 'exportFolder'? Where is it defined?


If you posted your entier script I wouldn't expect it to get past this line (which is why posting the error message is so useful).



do shell script

As a line by itself this makes no sense - do shell script requires a string paramater telling it the script to run (line breaks are important in AppleScript)


"exiftool -overwrite_original -Photoshop:CopyrightFlag='True'"


Assuming this is the parameter you're passing to do shell script there are several problems with it. First off is the lack of path to the executable. Now exiftool might be in your $PATH but you cannot rely on that so you s hould get in the hanit of using full paths.

Secondly, nowhere her are you telling exiftool which file(s) to run on, you're just running the above command once for each file in exportFolder (assuming exportFolder is valid (see above). I'd expect you to be passing in a file name for each iteration.



done



This line serves no purpose.


end repeat

This line's correct, though. 🙂


Overall, I'd expect your script to look more like:


set theFolder to (choose folder)

tell application "Finder" to set theFiles to files of theFolder as alias list

repeat with eachFile in theFiles

do shell script "/usr/bin/exiftool -overwrite_original -Photoshop:CopyrightFlag='True'" & quoted form of POSIX path of eachFile

end repeat


although, I'm not an expert with exiftool to know that's the right set of parameters, but at least you can see how to integrate the files into the command line.

Apr 22, 2011 8:32 AM in response to Tony T1

Error in my repeat loop that I realized after reading Camelot's correct response to the OP.

Just to correct the above:


tell application "Finder" to set f to files of exportFolder as alias list

repeat with i in f

do shell script "exiftool -overwrite_original -Photoshop:CopyrightFlag='True'" & space & quoted form of POSIX path of i

end repeat


Tony

Apr 22, 2011 1:10 PM in response to Tony T1

How to post code.

click on Use advanced editor. It's just above the reply box to the far right of the reply box.

click on the blue >>. It's next to the smiling face.

click on syntax highlighting.

click on plain


This will open a little box. Drop your code in to this box.



(* 
    Run in the Script Editor.
    Click on the Event Log tab to see the output from the log statement
    Click on Run


    For running shell commands see:
    http://developer.apple.com/mac/library/technotes/tn2002/tn2065.html

 *)

on run
    -- Write a message into the event log.
    log "  --- Starting on " & ((current date) as string) & " --- "



    -- Ping google's numeric address.  Avoids problems with DNS lookup
    -- -c1 means to ping one time.
    set fromUnix to do shell script "ping -c1 72.14.204.99"

    log fromUnix
    display dialog fromUnix giving up after 30


    set fromUnix to do shell script "ping -c1 google.com"

    log fromUnix
    display dialog fromUnix giving up after 30
end run


Comment: Instesting how they have languages listed that are never posted here. SQL!


I write the script posted here so the line are short.

( but not short enough any more. At least that infinite scroll to the right has gone away! You know one long long line in a post makes the post infinitely scroll to the right. )

------------------


Spell checking:


click on the abc check icon all the way to the right at the top of the post.


left click once on a miss spelled word to see corrections.


Did Apple do any useability testing on this software?


Robert

Apr 23, 2011 1:30 AM in response to Camelot

Camelot wrote:


repeat with i from 1 to count of items in exportFolder

What is 'exportFolder'? Where is it defined?


Thanks, 'exportFolder' is defined earlier in the script. The purpose of the script - which is run from within Aperture - is to rename the selected images, export them (to the 'exportFolder'), reset the version name and, using ExifTool, add metadata which Aperture does not write to exported JPEGs.


I have a version of this which works in Automator but I couldn't see any clues there on setting the path to ExifTool.


Here's the full (modified) script. Currently it returns an error

Result:

error "No file specified" number 1


so I now need to understand if I can use 'exportFolder' (defined at the start) to tell Finder which folder to use.


* * *


-- Creating filenames by making Version Name from the IPTC Headline and Filename (with hypens where spaces exist). The Aperture Version Name is used to create the file's name on export. Once exported the Aperture Version Name is reset to its original.


tell application "System Events"

set exportFolder to (choose folder with prompt "Choose an export folder")

end tell


tell application "Aperture"

set theSel to (get selection)

if theSel is {} then

error "Please select an image or two!."

else

repeat with theImg in theSel



-- Creating a new Aperture Version Name which will become the exported file's filename.


tell theImg

set headline to (get value of IPTC tag "Headline" of theImg)

set AppleScript's text item delimiters to " "

set theTextItems to text items of headline

set AppleScript's text item delimiters to "-"

set headline to theTextItems as string

set AppleScript's text item delimiters to {""}

set objectName to (get value of IPTC tag "ObjectName" of theImg)

set newVersion to (headline & "-" & objectName) as string

set name of theImg to newVersion

end tell

end repeat



-- Exporting the files as JPEGs to chosen folder/Project Name using the Version Name as a filename


export theSel using export setting "JPEG - Original Size" to exportFolder



-- Resetting the Aperture Version Name back to filename using IPTC Title (which should be the file's filename without suffex).


repeat with theImg in theSel

tell theImg

set title to (get value of IPTC tag "ObjectName" of theImg)

set name of theImg to title

end tell

end repeat


end if

end tell


--Using ExifTool to set Photoshop Copyright Status etc


tell application "Finder" to set theFiles to files of exportFolder as alias list

repeat with eachFile in theFiles

do shell script "/usr/bin/exiftool -overwrite_original -Photoshop:CopyrightFlag='True' -Photoshop:URL='http://davidgordon.co.uk/'" & quoted form of POSIX path of eachFile



end repeat

display dialog "Done that!" with iconnotebuttons "OK" default button 1 giving up after 10


* * *

May 30, 2013 10:26 AM in response to David Gordon

I am looking to write exiftool Artist feild to my images

This is the script I have been trying.


set theFolder to (choose folder)

tell application "Finder" to set theFiles to files of theFolder as alias list

repeat with eachFile in theFiles

do shell script "exiftool & quoted form of POSIX path of eachFile

-EXIF:Artist='Artist'"

end repeat



The script runs but never ends. What might have wrong in the script to have the choosed file run the script?

This is the script if I had code a file path

do shell script "exiftool ~/Desktop/Images/* -EXIF:Artist='Artist'"

May 30, 2013 11:02 AM in response to Jade Curtis

Your question has already been answered in the earlier replies to this post.


In short, your syntax is wrong:


do shell script "exiftool & quoted form of POSIX path of eachFile -EXIF:Artist='Artist'"


This will run the command exactly as written since all the text is quoted. exiftool has no idea how to interpret "... & quoted form of POSIX path of eachFile" since those are AppleScript commands, not valid in shell/exiftool


At the very, very least you need to change your quoting so that the AppleScript commands can be evaluated by AppleScript and the results passed to the shell:


do shell script "exiftool " & quoted form of POSIX path of eachFile & " -EXIF:Artist='Artist'"


Note how using the quotes and ampersands combines the shell commands with AppleScript data. The syntax highlighting should give you an idea of how this fits together.


This still won't work though. exiftool requires that the file be the LAST parameter, not stuck in the middle, so you now need to reorganize the components to make sense:


do shell script "/usr/bin/exiftool -EXIF:Artist='Artist' " & quoted form of POSIX path of eachFile

How do I run a shell script via AppleScript?

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