Strange problem calling AppleScript

I’ve got an AppleScript called RemoveLoginItem with the following code in my Xojo project:

on run (removeItem) tell application "System Events" if login item removeItem exists then delete login item removeItem end if end tell end run

When I call RemoveLoginItem(“item”), where “item” is a login item that I know is present, the script fails. It fails the test in the “if” statement, so doesn’t enter that block and remove the item.

However, if I change the code to:

set removeItem to "item" tell application "System Events" if login item removeItem exists then delete login item removeItem end if end tell

…and run the script manually, then it works just fine for the same item.

What’s going on? Is this a text encoding issue with the string being passed in the parameter, perhaps? (That would be weird, as I’m passing strings to half a dozen other AppleScripts in my project, and haven’t hit a snag like this with any of them.)

Bump!

I have removed the “if” block now, to no avail. It still fails to remove the login item when the name is passed in from Xojo:

on run (removeItem) tell application "System Events" try delete login item removeItem on error -- do nothing end try end tell end run

This also works fine if I modify it with a hard-coded string and then run the script in AppleScript Editor.

I have used “display dialog” in the script to show the string being passed by Xojo… it looks like what I expect it to be.

Any ideas?

Bump!

Still banging my head on this. The AppleScript works fine with a hard-coded string, but not with a string passed from Xojo. What’s up?

sigh

Well, I’ve lost a lot of confidence in Xojo’s ability to call into AppleScripts here. There’s just no good reason for this behavior that I can find. Which leaves me wondering if I can rely on the other scripts I’m using.

Fortunately, I’ve solved my own problem in this case by simply eliminating that AppleScript. Instead, I’m having Xojo run a shell script with an osascript command. Seems silly to need to use a shell script to run an AppleScript to get the behavior to work properly from Xojo, but there you have it.

If anyone can come up with an explanation for this weirdness, I’d still be interested.

I have a similar problem with this script :

set liste to "" tell application "Calendar" set lesEvent to every event of calendar "Work" repeat with i from 1 to length of lesEvent if (start date of item i of lesEvent) > (current date) then set liste to liste & start date of item i of lesEvent & "*" set liste to liste & summary of item i of lesEvent & "|" end if end repeat end tell return liste

It works fine in the editor but return an empty list when called from Xojo.

But if I remove the IF… THEN…, it works correctly in Xojo.

Can you provide an example of the string you’re passing in? I remember (but can’t find) having a similar problem caused by certain characters in the string being passed in that needed to be escaped but couldn’t escape them. In AppleScript Editor I could enter strings and see them in the result pane with special formatting but passing the strings in just wouldn’t work, the result pane showed something else. Don’t remember if I got it working but maybe someone else can spot a problem/complication with your string.

I think Will is right about your removeItem variable not being an acceptable Applescript formatted text object.

Try checking what class the removeItem variable is, or doing what might be an unnecessary coercion in your script, just to eliminate the chance that the removeItem is not being seen as a valid Applescript Text object.

Class checking example

if class of removeItem = text then
	return "True" as text
else
	return "False" as text
end if

Class coercion example

set removeItem to removeItem as text

It’s worth putting class coercions in a try block, and returning a error message as text to your Xojo App.

Regards Mark