"open" command not working

Hello folks… I’m trying to complete a nifty little script to put the date on the bottom right corner of all my pictures, and I’m having a small problem. No matter how I try, I can’t get the pictures to open in GraphicConverter. Here’s what I’m trying…


tell application "Finder"
	set pictureFolder to choose folder
	set listOfFiles to every file of pictureFolder
	set numberOfFiles to count of listOfFiles
end tell

repeat with counter from 1 to numberOfFiles
	tell application "GraphicConverter"
		open file (item counter of listOfFiles)
	end tell
	
	-- ...do stuff
end repeat

When you run that, the computer will open all the pictures… with whatever the default program is set to. If its preview, then they’ll all open in preview. I’ve tried

tell application "Finder"
	open file (item counter of listOfFiles) using "GraphicConverter"
end tell

but that doesn’t seem to work either. Any suggestions?

Goes like this:

tell application "GraphicConverter" to open alias ((path to pictures folder as text) & "anticrepuscular.jpg")

Note that the reference to the file is an alias.

Hi,

almost every application expects a string path or an alias for the open command
Try this:

set pictureFolder to choose folder
tell application "Finder" to set listOfFiles to every file of pictureFolder

repeat with thisFile in listOfFiles
	tell application "GraphicConverter"
		open (thisFile as string)
	end tell
	
	-- ...do stuff
end repeat

That seems to work… thanks!

Also, thanks for cleaning up the code! As a programmer from various other languages, I harbor a deep dislike for applescript and its idiosyncrasies, but I’m still interested enough to try things, and the only way I’ve found I can learn is to look at other code. Thanks!

I’d say the real problem here is that Finder doesn’t return aliases by default. People that haven’t realized this don’t know that they need to change the value some how.

Though why that is is quite beyond me.

The Finder is a scriptable application like any other, and scriptable applications normally work with object references. The real real problem is the chronic lack of documentation for most applications’ scripting interfaces, but this is hardly news.

Anyway, if you need aliases from Finder, you can get them using the ‘as alias list’ coercion (with some extra fiddling to avoid some known bugs in AppleScript):

set pictureFolder to choose folder
tell application "Finder"
	if (count every file of folder pictureFolder) = 1 then -- ('as alias list' coercion fails if there's only 1 item, so handle that case separately)
		set listOfFiles to {file 1 of folder pictureFolder as alias}
	else
		set listOfFiles to every file of folder pictureFolder as alias list
	end if
end tell

tell application "GraphicConverter"
	open listOfFiles
end tell

Alternatively, you could just ask Finder to open the files in GC for you:

set picturesFolder to choose folder
tell application "Finder"
	open every file of folder picturesFolder using (path to application "GraphicConverter")
end tell

HTH

has

Incidentally, since the OP is already familiar with other languages, here’s the Ruby+appscript version of the above script by way of comparison:

#!/usr/bin/env ruby

require 'osax'
include Appscript, OSAX

picture_folder = osax.choose_folder

list_of_files = app('Finder').folders[picture_folder].files.get(:result_type=>:alias)

app('GraphicConverter').open(list_of_files)

It’s a bit cleaner as Ruby doesn’t suffer AS’s coercion bugs so doesn’t need the extra workarounds. Using Perl/Python/Ruby/ObjC doesn’t make dealing with individual applications’ bugs and idiosyncrasies any easier, of course, but if the AppleScript language isn’t your thing then you may find one of the others a bit more comfortable to work in.

HTH

has