odd behavior from a script

so, i have a filemaker database that i built for work. i have the database where you can import pictures. but to keep everything ‘uniform’, i wrote an applescript that will check the file that is being added to see if it is a jpg. if it is, it will rename it, move it to our server drive and import it to filemaker. if it’s not a jpg, it will convert the file to jpg using image events, then continue the process with the jpg that was just created. it works like a charm in script editor. but when i add it to filemaker, i get a -1708 error that image events can’t continue launch. from what i’ve read online, it’s a handler problem…but…if it’s a handler…why is it working in script editor?? i’m confused and hoping someone here can help me out.


... (more script before here that checks and mounts the drive if needed)....

		if fileExtension is "jpg" then
			set stringFile to theFile1 as string
			set AppleScript's text item delimiters to {"."}
			set oldDelims to AppleScript's text item delimiters
			try
				set fileName1 to name of theFile1
				set stringFileName to fileName1 as string
				set nameWithoutExtension1 to terminalID & "_1"
				set newName1 to nameWithoutExtension1 & ".jpg"
				set name of theFile1 to newName1
				set AppleScript's text item delimiters to oldDelims
				set thePath to my GetParentPath(theFile1)
			on error
				set AppleScript's text item delimiters to oldDelims
			end try
			set imagePath1 to (thePath & newName1)
			move imagePath1 to fileLoc
			move imagePath1 to trash
		else
			display alert "This is not a jpg file.  You will need to convert it before you add it to the database.  Press 'OK' to convert the file."
			set this_file to theFile1
			set the target_file to (path to desktop) & fileName
			set the target_path to the target_file as Unicode text
			try
				tell application "Image Events"
					launch
					set this_image to open this_file
					save this_image as JPEG in target_path with icon
					close this_image
				end tell
			on error errMsg number errorNumber
				display dialog "An unknown error occurred:  " & errorNumber & " - " & errMsg as text
			end try
			set thePath to my GetParentPath(theFile1)
			set imagePath1 to (thePath & fileName)
			move imagePath1 to fileLoc
			move imagePath1 to trash
		end if

....(more script of other stuff)...


Model: iMac
AppleScript: 2.6.1
Browser: Chrome 45.0.2454.85
Operating System: Mac OS X (10.10)

What happens if you cut out the (unnecessary) launch?

Hello Shane

The original question let me think that the script is a folder action one.
If it’s the case, according to Apple sample scripts, launch is required for Image Events.
I checked that it’s always claimed as required in the scripts delivered with 10.10.X and 10.11.

Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) samedi 12 septembre 2015 12:11:31

Hi.

The ‘move’ commands suggest that whole of the posted code, including the Image Events stuff, is nested within a ‘tell’ statement for another application. Does it make any difference if the code’s tidied up to avoid this? Most of it’s vanilla AppleScript and doesn’t need to be addressed to a particular application. The exceptions are the Image Events stuff of course, the ‘move’ commands, and the ‘display alert’. In the last case, the alert will be displayed by the application “told” to display it, otherwise it’ll be the application running the script.

FWIW, it made me think it was stored within FileMaker and was therefore within an implied tell block.

Here are four instructions extracted from the posted code :

set fileName1 to name of theFile1
set name of theFile1 to newName1
set imagePath1 to (thePath & newName1)
move imagePath1 to fileLoc

Which application may execute them ?
As far as I know, neither Finder nor System Events may do that because they are trying to move imagePath1 which, if I read correctly, is a list.
Explanations :
I don’t know an app able to execute name of theFile1 if theFile1 is a string or an Unix path (which is a string) so, theFile1 must be an alias, a Finder reference or a «class furl».
If it’s an alias, imagePath1 is the list {the alias theFile1, newName1}
If it’s a Finder reference, imagePath1 is the list {Finder reference theFile1, newName1}
If it’s «class furl», imagePath1 is the list {«class furl» theFile1, newName1}

Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) samedi 12 septembre 2015 18:03:34

sorry, i was out for the weekend.

yes, the script works fine inside of script editor. it stops working once it’s run from inside of filemaker. (it’s not a folder action). and yes, there’s a tell application “Finder” to start it off due to the fact that it’s checking to make sure that the server drive is mounted first. here is the whole script in case you wanted to see the rest:


tell application "Finder"
	set uName to "**USERNAME_HERE**"
	set pass to "**PASSWORD_HERE**"
	if exists folder "Database" in desktop then
		delay 0.01
	else
		set mount1_conn to false
		set loopcount to 1
		repeat until mount1_conn is true or loopcount is greater than 3
			if exists folder "DRIVENAME" in desktop then
				set mount1_conn to true
			else
				try
					mount volume "afp://SERVERNAME.local/DRIVENAME" as user name uName with password pass
				end try
				delay 1
				set loopcount to loopcount + 1
			end if
		end repeat
	end if
	set terminalID to "**IDNUMBER**" as string
	set fileName to terminalID & "_1.jpg" as string
	set fileLoc to "DRIVENAME:FOLDERNAME:SUBFOLDERNAME:" as alias
	if exists file fileName in fileLoc then
		delay 0.01
	else
		set theFile1 to (choose file with prompt "Select Picture To Import")
		set fileExtension to name extension of theFile1
		if fileExtension is "jpg" then
			set stringFile to theFile1 as string
			set AppleScript's text item delimiters to {"."}
			set oldDelims to AppleScript's text item delimiters
			try
				set fileName1 to name of theFile1
				set stringFileName to fileName1 as string
				set nameWithoutExtension1 to terminalID & "_1"
				set newName1 to nameWithoutExtension1 & ".jpg"
				set name of theFile1 to newName1
				set AppleScript's text item delimiters to oldDelims
				set thePath to my GetParentPath(theFile1)
			on error
				set AppleScript's text item delimiters to oldDelims
			end try
			set imagePath1 to (thePath & newName1)
			move imagePath1 to fileLoc
			move imagePath1 to trash
		else
			display alert "This is not a jpg file.  You will need to convert it before you add it to the database.  Press 'OK' to convert the file."
			tell application "Finder"
				set this_file to theFile1
				set the target_file to (path to desktop) & fileName
				set the target_path to the target_file as Unicode text
				
				try
					tell application "Image Events"
						launch
						set this_image to open this_file
						save this_image as JPEG in target_path with icon
						close this_image
					end tell
				on error errMsg number errorNumber
					display dialog "An unknown error occurred:  " & errorNumber & " - " & errMsg as text
				end try
				set thePath to my GetParentPath(theFile1)
				set imagePath1 to (thePath & fileName)
				move imagePath1 to fileLoc
				move imagePath1 to trash
			end tell
		end if
	end if
end tell
on GetParentPath(theFile)
	tell application "Finder" to return container of theFile as text
end GetParentPath

ok, so weird turn of events. just found that it DOES work in filemaker…once. the second time i try it, it errors out. if i quit filemaker and try it again, it works again…once. so…would this be a filemaker issue or an applescript one?

With which kind of file were your tests done ?

I repeat that several instructions can’t be executed because the operator move doesn’t apply upon strings.

Other point, if you try to treat a jpeg file whose extension is “jpeg”, it will be treated as a non jpeg one because you test only the extension “jpg”

Here is a version with some comments and some changes
For my own use I would not speak to the Finder but to System Events which is more efficient but it would be an other game.


tell application "Finder"
	set uName to "**USERNAME_HERE**"
	set pass to "**PASSWORD_HERE**"
	if exists folder "Database" in desktop then
		delay 0.01
	else
		set mount1_conn to false
		set loopcount to 1
		repeat until mount1_conn is true or loopcount is greater than 3
			if exists folder "DRIVENAME" in desktop then
				set mount1_conn to true
			else
				try
					mount volume "afp://SERVERNAME.local/DRIVENAME" as user name uName with password pass
				end try
				delay 1
				set loopcount to loopcount + 1
			end if
		end repeat
	end if
	set terminalID to "**IDNUMBER**" -- as string # no need to coerce a string as string
	set fileName to terminalID & "_1.jpg" -- as string # no need to coerce a string as string
	set fileLoc to "DRIVENAME:FOLDERNAME:SUBFOLDERNAME:" as alias
	if exists file fileName in fileLoc then
		delay 0.01
	else
		set theFile1 to (choose file with prompt "Select Picture To Import")
		set fileExtension to name extension of theFile1
		# Here theFile1 is an alias
		# Here fileLoc is an alias
		--if fileExtension is "jpg" then
		if fileExtension is in {"jpg", "jpeg"} then #  EDITED because there is no need to convert a jpeg into a JPEG file
			set stringFile to theFile1 as string
			set oldDelims to AppleScript's text item delimiters # MOVED to its correct location
			set AppleScript's text item delimiters to {"."}
			-- set oldDelims to AppleScript's text item delimiters # WAS ERRONEOUSLY HERE
			try
				set fileName1 to name of theFile1 # Here fileName1 is a string
				-- set stringFileName to fileName1 as string # This variable was never used !
				set nameWithoutExtension1 to terminalID & "_1"
				set newName1 to nameWithoutExtension1 & ".jpg"
				set name of theFile1 to newName1
				# Now theFile1 is the renamed alias
				set AppleScript's text item delimiters to oldDelims
				--set thePath to my GetParentPath(theFile1) # here thePath is a string
			on error
				set AppleScript's text item delimiters to oldDelims
			end try
			(*
			set imagePath1 to thePath & newName1 # Here imagePath1 is a string
			set imagePath1 to imagePath1 as alias # ADDED
			move imagePath1 to fileLoc
			move imagePath1 to trash
			*)
			move theFile1 to fileLoc
			move theFile1 to trash
		else
			display alert "This is not a jpg file.  You will need to convert it before you add it to the database.  Press 'OK' to convert the file."
			tell application "Finder"
				set this_file to theFile1
				-- set the target_file to (path to desktop) & fileName
				-- set the target_path to the target_file as Unicode text
				set the target_path to (path to desktop as text) & fileName # This single instruction does the job
				try
					tell application "Image Events"
						launch
						set this_image to open this_file
						save this_image as JPEG in target_path with icon
						close this_image
					end tell
				on error errMsg number errorNumber
					display dialog "An unknown error occurred:  " & errorNumber & " - " & errMsg as text
				end try
				set thePath to my GetParentPath(theFile1)
				set imagePath1 to thePath & fileName # Here, imagePath1 is a string
				set imagePath1 to imagePath1 as alias # ADDED
				move imagePath1 to fileLoc
				move imagePath1 to trash
			end tell
		end if
	end if
end tell

on GetParentPath(theFile)
	tell application "Finder" to return container of theFile as text
end GetParentPath

To prove what I wrote, here is the event log :

tell application "Finder"
	set name of alias "SSD 500:Users:<theHomeFolder>:Desktop:How to Make a Bootable OS X El Kapitan.rtfd:" to "How to Make a Bootable OS X El Capitan.rtfd"
end tell
--Résultat :
alias "SSD 500:Users:<theHomeFolder>:Desktop:How to Make a Bootable OS X El Capitan.rtfd:"

returned when I ran :

set aFile to (path to desktop as text) & "How to Make a Bootable OS X El Kapitan.rtfd:" as alias

tell application "Finder"
	set name of aFile to "How to Make a Bootable OS X El Capitan.rtfd"
end tell
aFile
tell a

Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) lundi 14 septembre 2015 21:43:38

thank you for the response. i tried your script and it’s still coming up with the same error. so at this point, i think it might actually be filemaker that’s the problem, not applescript. but i learned a lot from what you just showed me. thank you for that.

Is the instruction
Say “I’am here”
accepted when the script is launched from FileMaker ?

If it is, you may insert some instructions:
Say “I’am at point 1”

Say “I’am at point 2”
.

In several locations in the script.
At first run every messages will be said but at second one you will perhaps ear some of them helping us to discover what fails.

I don’t understand - but maybe it’s because I am tired - what’s the need for the instructions playing with the text item delimiters.

It may be a good idea to try to disable them (with two dashes at front).

I’m sure that launch is required when we trigger Image Events in a folder action script.
May be a good idea to disable this instruction too.
I’m wondering if it’s the fact to launch it several times which hurt Filemaker.

May help to edit a bit this way :


tell me to set the target_path to (path to desktop as text) & fileName # This single instruction does the job
tell application "System Events"
	set needLaunch to "Image Events" is not in (name of processes)
end tell
try
	tell application "Image Events"
		if needLaunch then launch
		set this_image to open this_file
		save this_image as JPEG in target_path with icon
		close this_image
	end tell
on error errMsg number errorNumber
	display dialog "An unknown error occurred: " & errorNumber & " - " & errMsg as text
end try

As I wrote, I’m tired, will be back tomorrow.

Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) lundi 14 septembre 2015 22:53:53

yes, having the script speak is accepted in filemaker

i will try to add the spoken break points and see what happens

the text delimiters were in the script that i found online to help me with finding the file name. if they aren’t needed, i can take those out. i’m far from an expert in applescript. so, i’m learning as i go.

i will try to add that new portion of script and keep you posted. thank you again for all your help!

THAT WORKED! thank you so much. i guess the image service was stuck open after the first import. now it will work every time. thanks again!!

Thanks for the feedback.
If I understand well, it’s the fact to no longer triggering Launch Image Events if it was already available in the events which does the trick.

If it’s really that it may be simpler to quit Image Events after every use:

  tell application "Image Events"
                       launch
                       set this_image to open this_file
                       save this_image as JPEG in target_path with icon
                       close this_image
quit
                   end tell

I tested this piece of code and on exit, the process “Image Events” is no longer loaded.

When I will have your feedback, I will try to apply some cleaning to the code.

Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) mardi 15 septembre 2015 10:52:33

just tested. that worked too.

Did you tried to disable the instructions related to text item delimiters ?

Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) mardi 15 septembre 2015 17:44:31

no, it worked with it in there, so i didn’t mess with it. lol. but let me try that now.

yes, it works without it.

Thanks.

I guessed that but better to check before removing them.

Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) mardi 15 septembre 2015 18:09:20

Oops

I discovered what may be a serious error.

When the script execute :

move theFile1 to fileLoc
move theFile1 to trash

if theFile1 was on the same volume than fileloc,
the file is not stored in the folder fileLoc, it is moved to the trash.

Explanations:

the script defines fileLoc by :
set fileLoc to “DRIVENAME:FOLDERNAME:SUBFOLDERNAME:” as alias
it defines theFile as
(“path:to:the:chosen:folder”&“wxyz.jpg”) as alias
when the instruction
move theFile1 to fileLoc
is executed, the file is correctly moved to the folder but what was forgotten is that after that, the variable theFile is changed. it’s now :
(“DRIVENAME:FOLDERNAME:SUBFOLDERNAME:” & “wxyz.jpg”) as alias
so, the 2nd instruction moves it to the trash.

At least it’s what I get with items stored on local devices.

As I am editing the code to replace the Finder by System Events I discovered a funny feature resembling to a bug. When I execute :

tell application “System Events”
move aFileDefinedAsAlias to aFolderOnAnOtherDisk
end

the variable aFileDefinedAsAlias contain an awful monster
alias “myHD:Users:home:Desktop:aFile”
becomes
alias “/User/home/Desktop/aFile”

In your case it seems that the deletion will not strike because fileLoc is not on the same device than theFile1 but I’m not sure of that.

Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) mardi 15 septembre 2015 20:53:35