On the exiftool home page there are some downloadable example OS X scripts (droplets). The first droplet bundles exiftool as an application with the script, so you can use this as an example.
@boardhead
I don’t think the first droplet bundles exiftool in the script. I think the droplet doesn’t use exiftool at all. Here is it’s code:
-- this script modified from an original script written by Brett Gross - PH
-- ------------------------------------------------------- GLOBAL VARIABLES
global extract
global theVers
on setup()
try
set mePath to path to me
set mePathPOSIX to POSIX path of mePath
set extract to (mePathPOSIX & "Contents/Resources/extract_preview") as string
--display dialog
-- Debug code to make sure that we've got things working alright
set versionComm to " -ver"
set theScript to (quoted form of extract & versionComm) as string
--display dialog theScript
set theVers to do shell script theScript
display dialog theVers buttons {"OK"} default button 1 giving up after 2
return theVers
on error
-- Abort
return "Err"
end try
end setup
on extractPreview(theImage)
-- Pass an alias
set theInfo to (info for theImage)
set theName to name of theInfo
set ppath to (POSIX path of theImage)
set basePath to (characters 1 thru ((length of ppath) - (length of theName)) of ppath) as string
log basePath
set baseName to (characters 1 thru ((length of theName) - 4) of theName) as string
log baseName
set outFile to ((baseName & "_preview.jpg") as string)
log outFile
set theScript to ((quoted form of extract) & " " & (quoted form of POSIX path of theImage) & " '" & basePath & outFile & "'") as string
set theRes to do shell script theScript
log theScript
return (basePath & outFile) as string
end extractPreview
on procFiles(theFiles)
repeat with curFile in theFiles
-- Go ahead and process the file
extractPreview(curFile)
end repeat
end procFiles
on run
set theVers to my setup()
if theVers is not "Err" then
set theFiles to (choose file with multiple selections allowed) as list
my procFiles(theFiles)
end if
end run
on open (docList)
set theVers to my setup()
if theVers is not "Err" then
my procFiles(docList)
end if
end open
on open (theFiles) --actions as droplet
extractXMP(theFiles)
end open
on run --actions with normal use
set theFiles to (choose file with multiple selections allowed without invisibles)
extractXMP(theFiles)
end run
on extractXMP(theFileList)
repeat with theFile in theFileList
--save name and path
set theFilePath to POSIX path of theFile
set theInfo to info for theFile
set theFileName to name of theInfo
--save exiftool command and execute it
set mePath to path to me
set mePathPOSIX to POSIX path of mePath
set exiftoolPath to (mePathPOSIX & "Contents/Resources/exiftool") as string
set theShellCommand to exiftoolPath & " -XMP -b" & space & theFilePath
set theXMPcontents to do shell script theShellCommand
--change extension
set theFileNameChopped to text 1 thru -4 of theFileName
set theFileName to theFileNameChopped & "xmp"
--write xmp file
set textFile to open for access ((path to desktop folder as text) & theFileName) with write permission
try
set eof of textFile to 0
write theXMPcontents to textFile
close access textFile
on error e
close access textFile
display dialog e
end try
end repeat
end extractXMP
But I have still two questions.
First, when I use the file by double clicking it, I get a warning saying if I am sure I want to stop the file or execute it.
Is there a way to get rid of this?
Second, when I make an xmp file with this script the it’s file kind is “BBEdit text document”. I have another xmp file with “Adobe XMP file” as file kind. Can I change the file kind with applescript?
I’ve moved the applescript applescript, and now I get the error -1409.
Somebody can explain this error in plain english what the error means?
the line where I write the xmp file seems to be the problem
if you use a string path specifier to open a file for read/write operation, you should add the keyword file
and you can omit folder in path to desktop folder
set textFile to open for access file ((path to desktop as text) & theFileName) with write permission
when I use quoted form the outputPath of the name of the xmp file has quotes. But it is still located at the root of my disk and still the filename is the path of where I want the file to be.
ps: I think the problem is not the path in the exiftool command like in your example, but the path to write the file.
Here is the script as I have it now.
(difference with the previous posted version is I added a piece of code where I placed the xmp file in the same location as the pdf file where i get the xmp from)
on open (theFiles) --acties bij gebruik als droplet
extractXMP(theFiles)
end open
on run --acties bij gewoon gebruik
set theFiles to (choose file with multiple selections allowed without invisibles)
extractXMP(theFiles)
end run
on extractXMP(theFileList)
repeat with theFile in theFileList
--naam en pad opslaan
set theFilePath to POSIX path of theFile
set theInfo to info for theFile
set theFileName to name of theInfo
--exiftool commando opstellen en uitvoeren
set mePath to path to me
set mePathPOSIX to POSIX path of mePath
set exiftoolPath to (mePathPOSIX & "Contents/Resources/exiftool") as string
set theShellCommand to exiftoolPath & " -XMP -b" & space & quoted form of theFilePath
set theXMPcontents to do shell script theShellCommand
--extensie veranderen
set theFileNameChopped to text 1 thru -4 of theFileName
set theFileName to theFileNameChopped & "xmp"
if theXMPcontents is not equal to "" then
--pad opstellen
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set stringlist to every text item of theFilePath
set listnr to number of items in stringlist
set stringlist to items 1 thru (listnr - 1) of stringlist
set folderPath to stringlist as string
set AppleScript's text item delimiters to ASTID
set outputPath to folderPath & "/" & theFileName
display dialog outputPath
--xmp bestand schrijven
set textFile to open for access file (quoted form of outputPath) with write permission
try
set eof of textFile to 0
write theXMPcontents to textFile
close access textFile
on error e
close access textFile
display dialog e
end try
else
display dialog "Er zit geen XMP file in " & theFileNameChopped
end if
end repeat
end extractXMP
This is the piece of code a added to the script
--pad opstellen
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set stringlist to every text item of theFilePath
set listnr to number of items in stringlist
set stringlist to items 1 thru (listnr - 1) of stringlist
set folderPath to stringlist as string
set AppleScript's text item delimiters to ASTID
set outputPath to folderPath & "/" & theFileName
display dialog outputPath
--xmp bestand schrijven
set textFile to open for access file (quoted form of outputPath) with write permission
not the filename, but the file path and only in shell script lines.
If the short name of the current user contains a space character or the app is placed in Applications Support folder the shell script line will fail. You have to escpace any space and special character (≠alphanumeric) in POSIX paths.
I said I used the quoted form of the filename, but I meant the file path.
And the script I posted is an old version. (updated now)
No spaces or special characters in the path
outputPath = /Users/jonasyde/School/Eindwerk/extract_xmp/sample_file.xmp
tested with files on desktop, doesn’t work either
sow, I think I made a fault in this line: (because it used to work before I added the next pieces)
set textFile to open for access file (quoted form of outputPath) with write permission
or in de building of the outputPath:
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set stringlist to every text item of theFilePath
set listnr to number of items in stringlist
set stringlist to items 1 thru (listnr - 1) of stringlist
set folderPath to stringlist as string
set AppleScript's text item delimiters to ASTID
set outputPath to folderPath & "/" & theFileName
and for competion, the full script: (last version, dubbelchecked)
on open (theFiles) --acties bij gebruik als droplet
extractXMP(theFiles)
end open
on run --acties bij gewoon gebruik
set theFiles to (choose file with multiple selections allowed without invisibles)
extractXMP(theFiles)
end run
on extractXMP(theFileList)
repeat with theFile in theFileList
--naam en pad opslaan
set theFilePath to POSIX path of theFile
set theInfo to info for theFile
set theFileName to name of theInfo
--exiftool commando opstellen en uitvoeren
set mePath to path to me
set mePathPOSIX to POSIX path of mePath
set exiftoolPath to (mePathPOSIX & "Contents/Resources/exiftool") as string
set theShellCommand to exiftoolPath & " -XMP -b" & space & quoted form of theFilePath
set theXMPcontents to do shell script theShellCommand
--extensie veranderen
set theFileNameChopped to text 1 thru -4 of theFileName
set theFileName to theFileNameChopped & "xmp"
if theXMPcontents is not equal to "" then
--pad opstellen
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set stringlist to every text item of theFilePath
set listnr to number of items in stringlist
set stringlist to items 1 thru (listnr - 1) of stringlist
set folderPath to stringlist as string
set AppleScript's text item delimiters to ASTID
set outputPath to folderPath & "/" & theFileName
display dialog outputPath
--xmp bestand schrijven
set textFile to open for access file (quoted form of outputPath) with write permission
try
set eof of textFile to 0
write theXMPcontents to textFile
close access textFile
on error e
close access textFile
display dialog e
end try
else
display dialog "Er zit geen XMP file in " & theFileNameChopped
end if
end repeat
end extractXMP
If I use this line of code it works, but when I use the next code, it doesn’t.
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set stringlist to every text item of theFilePath
set listnr to number of items in stringlist
set stringlist to items 1 thru (listnr - 1) of stringlist
set folderPath to stringlist as string
set AppleScript's text item delimiters to ASTID
set outputPath to folderPath & "/" & theFileName
display dialog outputPath
--xmp bestand schrijven
set textFile to open for access file (quoted form of outputPath) with write permission