Multiple selections for destination?

Hi, I’m new to both Applescript and this forum, so if I’m describing things incorrectly, and/or have this in the wrong place, please just let me know and I’ll happily change as needed.

I did try searching the forum first to see if I could find what I needed, but didn’t come up with anything I could then use, which is likely more down to my lack of experience than anything else - apologies if I missed the obvious!

I’ve written a script embedded within Automator, so wasn’t sure whether to post under automator, or here, but chose here as it’s more a script than an automator action. I’m really only using Automator to put some dialogue on the front and back of what’s essentially a script. This is because as far as I can see, Automator (at least under Mac OS X 10.6.8, which I’m using) doesn’t allow selection of both source and destination files to carry out an action - if I’m wrong on that, please correct me, as that would probably be easier for me to set up.

The script I’ve written is as follows -

on run {input, parameters}
	set exftarg1 to " -tagsfromfile "
	set exftarg2 to " '-IPTC:Keywords<IPTC:Keywords' '-XMP-dc:Subject<IPTC:Keywords' "
	set s to quoted form of POSIX path of (choose file with prompt "CHOOSE SOURCE FILE" without invisibles)
	set d to quoted form of POSIX path of (choose file with prompt "CHOOSE FILES TO CHANGE" with multiple selections allowed)
	do shell script "exiftool" & exftarg1 & s & exftarg2 & d
	return input
end run

And it seems to work - almost! The problem I’m having is that I can use my script to edit one destination file, but cannot edit multiple destination files even though I have

on the d (destination) choice.

I’m using exiftool to edit the metadata of various images, so the script is built around that.
First I’m setting 2 variables with the contents of argument files to go into exiftool so it can process the commands.
Then I set “s” to allow me to select a source file in Finder from which to obtain the image metadata I want.
Then I set “d” to allow me to select the destination files in Finder into which I want to write that image metadata.
Then I execute exiftool via a shell script.

Note that exiftool is capable of handling multiple files passed to it, and I’ve tested that both on the Terminal command line directly and in various Automator apps (via “for f in “$@”’ combined with '”$f"') , so feel reasonably confident it’s not exiftool that’s causing the problem.

Assuming it’s not exiftool, when running the workflow in Automator everything works until it gets to this line -

	
set d to quoted form of POSIX path of (choose file with prompt "CHOOSE FILES TO CHANGE" with multiple selections allowed)

If I select just one file to write metadata to, it works, executing exiftool to correctly put the metadata into the image tags I specified in exftarg2.
If I select more than one image at that point, which it allows, the following error is returned -

and does not execute the exiftool command, so no files are changed. As it’s a syntax error, I’m thinking the problem is with Applescript rather than exiftool.

It seems I’m missing something, as even though I think that with multiple selections allowed it should work for multiple files, it doesn’t, and when it returns the error, the path it shows is correct, even though it states it can’t get it.

I’ve now spent hours trying variations of this and getting no further, so if anyone can enlighten me on my mistakes, and provide examples of what I should do to correct it, I’d be very grateful.
Thanks,
Adrian

Model: Macbook Air model 3,2
AppleScript: 2.3
Browser: Safari 5.1.10 (6534.59.10)
Operating System: Mac OS X (10.6)

Hi,

choose file . with multiple selections allowed returns a list of alias specifiers
Assuming exiftool expects multiple file paths separated by space characters
you have to convert the list into the required format, for example


set chosenFiles to choose file with multiple selections allowed
set fileList to {}
repeat with aFile in chosenFiles
	-- coerces the alias to POSIX path and quote it
	set end of fileList to quoted form of POSIX path of aFile
end repeat
set {TID, text item delimiters} to {text item delimiters, space}
-- converts the list to a plain string space separated
set shellParameters to fileList as text
set text item delimiters to TID

Hi Stefan,

Many thanks for your prompt and helpful reply.

I translated your suggestion into my script as follows -

on run {input, parameters}
	set exftarg1 to " -tagsfromfile "
	set exftarg2 to " '-IPTC:Keywords<IPTC:Keywords' '-XMP-dc:Subject<IPTC:Keywords' "
	set s to quoted form of POSIX path of (choose file with prompt "CHOOSE SOURCE FILE" without invisibles)
	set chosenFiles to (choose file with prompt "CHOOSE FILE TO CHANGE" with multiple selections allowed)
	set fileList to {}
	repeat with aFile in chosenFiles
		-- coerce the alias to POSIX path and quote it
		set end of fileList to quoted form of POSIX path of aFile
	end repeat
	set {TID, text item delimiters} to {text item delimiters, space}
	-- converts the list to a plain string space separated
	set shellParameters to fileList as text
	set text item delimiters to TID
	do shell script "exiftool" & exftarg1 & s & exftarg2 & fileList
	return input
end run

I ran it as a workflow within Automator and received the following error message -

Ignoring the first, warning part of the error for the moment as that’s to do with the source file, focusing on the second part, it seems that the 2 files I selected in Finder are being concatenated as one file name, so it looks like I’ve not managed to put spaces between the names.
This is probably due to my incorrect translation of what you provided, so if you or anyone else can look over my code and see what I’m doing wrong, I’d be very grateful. I tried looking in the Language Guide, but couldn’t see anything wrong from that.

I can see what you’re saying about exiftool’s input format expectations, and it may not even work with text input, so I’ll probably need to go to that forum and ask what it expects. I’m not sure what classes other than text can be declared for shellParameters, and when I searched on the forum in as many places as looked appropriate, nothing was returned to explain. Meanwhile, I thought I’d ask here if my code is correct, so I can narrow down which problems belong to Applescript, and which to exiftool. If anyone has suggestions about whether text or something else would be the most appropriate shellParameter, I’d be grateful. What I’ve done in the past is use Automator’s Ask for Finder Items to input to exiftool, and that’s always worked perfectly well - not sure if that gives any clue what exiftool may expect?

Concerning the first part of the error (warning about PrintIM header) to do with the source file, I’m really not sure what’s going on there, as I didn’t change that part of the script, and it did not return a warning on the source in any prior version of the script. Perhaps I’m now mixing Applescript commands that don’t mix between what I’ve done for the source file, and what I’m doing for the destination files?
Again if anyone has any comments on that, I’d be very grateful.

My appreciation in advance for any help or suggestions,
Adrian

the result of the repeat loop is in the variable shellParameters, so it’s supposed to be


do shell script "exiftool" & exftarg1 & s & exftarg2 & shellParameters

Stephan,

THANK YOU, I tried it and it worked perfectly first time on 2 images - great!

Shows I still have much to learn about Applescript, as I thought the result of the repeat loop was in fileList - hmm!

Thanks again, that’s saved me a lot of work with it all :slight_smile: ,
Adrian

My Apologies Stefan for spelling your name wrongly - didn’t realize until later.

Thanks again for your help, it made a big difference, both to the script and my overall understanding,
Adrian