Problem making Applescript "run on argv".

Hello all,

I’m in the process of setting up a script that opens images in Photoshop, resaves them using a PS action while suppressing any errors/warnings, and logging them to a file. Thanks to “Muppet Mark” from the Adobe Forums for posting this script http://forums.adobe.com/thread/423794.

The script is set to ask for the file you want to work on. I need to modify this to get the file name from an argument coming from Command line. I’m unable to get it to work with “on run argv”. It throws an error on compile saying the command is already used more than once.

Can anyone figure this out? You would have my eternal gratitude. Thanks for looking.

Regards,
Daniel Costello

property Error_Report : (path to desktop folder as Unicode text) & "Problem Image Report.txt"

global PS_Info

set PS_Info to ""

--

set The_File to choose file

--

tell application "Adobe Photoshop CS2"

activate

try

with timeout of 2 seconds

open The_File

end timeout

on error

my Check_For_Dialogs()

tell application "System Events"

tell application process "Adobe Photoshop CS2"

keystroke return

end tell

my Problem_Images(The_File, PS_Info)

end tell

end try

if exists document 1 then

set Doc_Ref to the current document

tell Doc_Ref

      do action "LoadSave2" from "MediaBeacon"

end tell

end if

end tell

--

on Check_For_Dialogs()

-- This should catch the text of a Photoshop warning dialog

try

tell application "System Events"

if UI elements enabled then

tell window 1 of application process "Adobe Photoshop CS2"

if title contains "Adobe Photoshop" then set PS_Info to value of static text 2

end tell

else

tell application "System Preferences"

activate

set current pane to pane "com.apple.preference.universalaccess"

display dialog "UI element scripting is not enabled. Check \"Enable access for assistive devices\""

end tell

end if

end tell

return PS_Info

-- This should catch an error kicked back to AppleScript

on error eM number eN

set PS_Info to "Error: " & eN & ". " & eM

return PS_Info

end try

end Check_For_Dialogs

--

on Problem_Images(The_File, PS_Info)

-- Whatever info about the file you want a record of.

set File_Name to name of (info for The_File)

-- String of records to write to "Problem Image Report" text file.

set The_Info to File_Name & tab & PS_Info & return

try

open for access file the Error_Report with write permission

write The_Info to file the Error_Report starting at eof

close access file the Error_Report

on error

close access file the Error_Report

end try

end Problem_Images

Well the way you would do it is like this

on run argv
	if (count argv) is 0 then
		set The_File to choose file
	else
		set The_File to item 1 of argv
	end if
end run

The problem though is that your script has dialogs and other components that have interaction and AppleScripts ran from the terminal do not allow any user interaction and will error. So you either need to bypass any code that would require interaction OR you need to work up some trickery of running the interactive portion from their own scripts and launching them with AppleScript Runner

Thanks for the help James. Make the interaction parts as separate scripts sounds scary, but I’ll look into it.

I have been able to get it to run as an application now when dragging and dropping a file onto it. Perhaps I’ll just have to make the automated drag and drop osascript so its as if it was actually dragged and dropped… Hmm… Wish me luck.

Daniel