Passing variables between Applescript and Livecode

Deploying to Mac OS? Ask Mac OS specific questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
rodneyt
Posts: 128
Joined: Wed Oct 17, 2018 7:32 am

Passing variables between Applescript and Livecode

Post by rodneyt » Sat Aug 07, 2021 4:23 am

See also this thread: viewtopic.php?f=19&t=30552&p=164669&hil ... nt#p164669

I'm looking for advice or suggestions on the best way to pass variables between Applescript and Livecode.

Let's say I have created a Service menu item in Automator (see thread: viewtopic.php?f=9&t=36167)
I can then assign a keyboard shortcut system wide. If user was (say) in a browser or mail app, they could select some text and hit the keyboard shortcut and service menu applescript will invoke.

In my example below I show how you can grab the context of the user (which app is frontmost) and what text they have selected in that app.

But how can I pass this text to Livecode? The method of using dispatch works for passing a simple string (like the active app) but is not so great for passing say a large block of text (where you really need to pass a variable).

There should be a way to dispatch an appleevent to Livecode, but I haven't seen a workable example of how to do this. I'm aware that there are some extra steps to get this working in a standalone, for now, I am first interested in getting this working in the IDE.

Code: Select all

on run {input, parameters}
	-- get the frontmost app so we can interrogate if needed (e.g. browser) for user context
	tell application "System Events"
		set activeApp to name of first application process whose frontmost is true
	end tell
	
	-- TEST script to show input string user has selected when they invoked the service menu item.  How can I pass this variable to LIvecode?
	set tUserSelectedText to input as string
	display dialog tUserSelectedText
	
	tell application "LiveCode VERSION"
		activate
		do script "dispatch \"MYHANDLER\" to stack \"MYSTACK\" with " & quote & activeApp & quote
	end tell
	
	return input
end run


rodneyt
Posts: 128
Joined: Wed Oct 17, 2018 7:32 am

Re: Passing variables between Applescript and Livecode

Post by rodneyt » Sat Aug 07, 2021 11:42 am

(Updated post)

Here a working example of an Automator service sending an event to Livecode (and livecode requesting a variable from Applescript) and how to call a handler from the external applescript (passing through a short string variable)

This is working fine now.

If you run into problems with events throwing errors in Livecode, I suggest try first in an empty stack to confirm that everything is working properly, then try in a more complex stack. If you are still having problems (and it works in empty stack) try moving the Applescript to a frontscript.

Code: Select all

on run {input, parameters}
	-- get the frontmost app so we can interrogate if needed (e.g. browser) for user context
	tell application "System Events"
		set activeApp to name of first application process whose frontmost is true
	end tell

	tell application "LiveCode VERSIONHERE"
		activate
		try
			«event aevt1234» input
			catch
		end try
		#if you also want to call a handler - for example to pass through the active App the user was in, you can do it like this
		do script "dispatch \"MYHANDLER\" to stack \"MYSTACK\" with " & quote & activeApp & quote
	end tell
	
	return input
end run
I'm surrounding the event sending in a try/catch construct as at the moment that is throwing an error, even though it is working fine.

Then in Livecode card script:

Code: Select all


on appleEvent theClass, theID
   local myVar
   if (theClass = "aevt") then 
      put "class:" && theClass & return & "ID" && theID & return into myVar
      request appleEvent data
      put "AppleEvent data:" && it after myVar
      answer myVar
   end if
   pass AppleEvent
end appleEvent
I can then invoke this service item on a text selection (in this case within Apple Mail) and it works.

PaulDaMacMan
Posts: 616
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: Passing variables between Applescript and Livecode

Post by PaulDaMacMan » Fri Aug 13, 2021 3:22 am

Hah! Cool, I found this trying to find the thread about the AppleScript bug, that you already linked to, to answer the other thread you started about this!

Glad to see you got it working!
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

rodneyt
Posts: 128
Joined: Wed Oct 17, 2018 7:32 am

Re: Passing variables between Applescript and Livecode

Post by rodneyt » Fri Aug 13, 2021 3:32 am

One additional note for anyone reading this forum, be aware that calling Livecode handlers from Applescript (as opposed to sending events) requires you to enable scripting when you build your standalone. And furthermore I think for security reasons the "do script" construct won't work. This is discussed in the thread I linked in the first post above, but is a gotcha for beginners as you could build something in Livecode that will work fine in the IDE and then break when you build your standalone.

~ R

PaulDaMacMan
Posts: 616
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: Passing variables between Applescript and Livecode

Post by PaulDaMacMan » Fri Aug 13, 2021 10:10 pm

rodneyt wrote:
Fri Aug 13, 2021 3:32 am
One additional note for anyone reading this forum, be aware that calling Livecode handlers from Applescript (as opposed to sending events) requires you to enable scripting when you build your standalone. And furthermore I think for security reasons the "do script" construct won't work. This is discussed in the thread I linked in the first post above, but is a gotcha for beginners as you could build something in Livecode that will work fine in the IDE and then break when you build your standalone.

~ R
Yes Brian Milby provided the needed steps there.
And as I said you can use the AppleEvent (<<aecodehere>>) and then it doesn't seem to matter if your app is properly setup as a scriptable app, with plist keys & terminology definitions, or not.
The custom plist I had made had a key that made my standalone not-show up in the macOS Dock, so that it behaved like any other status-menubar type utility app.
key: LSUIElement value: 1 or YES for hidden from Dock.
The key NSUIElement was used prior to 10.5 Leopard (curious why they changed it from NS (NextStep) to LS (???).
https://stackoverflow.com/questions/620 ... -dock-icon
https://stackoverflow.com/questions/679 ... -dock-icon
https://developer.apple.com/library/arc ... sKeys.html
bwmilby wrote:
Thu Feb 01, 2018 11:25 pm
That should have been fixed. What version are you using? I’ll dig up the PR and check that nothing has changed.
----
Edit:

Just checked... in DP11 the .sdef file is in the standalone app but is getting stripped out for some reason. The .plist file is also missing the key needed to reference the file. The first key is already there, but the second needs to be added. If you don't specify a .sdef file or specify one that is missing, then you get no dictionary support. The app can still respond to events, you just need to dispatch them using the special code (I'd have to look up the specifics, but I think they are here in the forums).

Code: Select all

	<key>NSAppleScriptEnabled</key>
	<string>YES</string>
	<key>OSAScriptingDefinition</key>
	<string>terminology.sdef</string>
Edit2:

I finally read your second post correctly... Yes, defining a custom plist could cause an issue. What you need to ensure is present are the keys listed above. And also that the terminology.sdef file is located in the Resources folder which is in the same spot as the plist in the app (App/Contents/Resources).
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

Post Reply

Return to “Mac OS”