Is there a way for the Xojo app to receive multiple returns from an AppleScript to drive a progress bar? It seems that as soon as the first return is received the connection is closed. Searched forum and web but (apparently) no cigar. Thank you for any direction you can provide.
Nope.
You can call the script several times.
Eric, thank you for succinctly confirming my fears
Michel, thank you for your thoughts. My script is long and takes minutes to run so I couldnât figure out how to get the progress of running it once by calling it multiple times. Iâm probably missing something.
My workaround:
1) AppleScript writes to a text file at the end of every loop
2) Xojo app repeatedly reads that text file, converts to integer
3) app uses integer to drive progress bar
Not as elegant as I might have liked but it seems to work.
Another idea is to write progress output to a Named Pipe and read that from within Xojo. I couldnât find a complete solution but here are some links to get you started:
Thanks Mike. Looks like this might be batting above my average but Iâll dive in anyway. Iâm intrigued. I appreciate it.
Wait, maybe I misunderstood. How are you running the AppleScript? Sounds like youâre launching it as an app, which will work, as youâve discovered - my âNo.â was referring to the scenario when you include the AppleScript in your Xojo project and call it from code.
Canât you write âyour scriptâ in Xojo ?
I stopped using AppleScript because AppleScript is slow, very slow. Depending on what your script do, you may use Xojo to do that far faster⊠probably.
AppleScript is slow and frustrating and I curse it daily but its superpower is that you can control multiple scriptable apps at the same time - and in concert. Thatâs magical to me. AS particularly sucks when it comes to things like the UI and that (among other things) is what Iâm hoping to use Xojo for. I like it better than Swift or ASObjC. But Iâm just a hobbyist so take what I say with a grain of salt.
Thanks Eric. You were right. Iâm calling from code and apparently there is no way to receive multiple returns from a single script called once. Thank you.
Thereâs also the possibility to write your Xojo app with AppleEvents rather than scripts; thatâs what I usually do.
Every command you send in your script can be âconvertedâ to its AppleEvent equivalent. Then showing the progress is straightforward, as your Xojo app calls each bit of what was in the script (and can update the progress between the calls).
Yes - AppleScripts are run synchronously on the main thread. All Xojo code stops until the AS returns.
You could look into using the shell command âosascriptâ in an asychronous shell. That would allow Xojo to continue while the script executes and returns progress info.
Thank you Arnaud and Eric. While converting my current project might not be practical Iâm very interested to start writing AppleEvents in Xojo/osascript via shell commands.
Again, thank you all for being so helpful.
Unless your AppleScript is trivial - for example, one or two commands - I wouldnât bother converting it to Apple Events. Theyâre much more difficult to understand and maintain, and donât take advantage of a big chunk of the data conversion AppleScript silently handles behind the scenes.
Furthermore, Apple Events are not faster than AppleScript in any appreciable way and Xojoâs Apple Event support is poor in many ways. Iâd suggest you keep up using the scripts you already have.
You can use the âosascriptâ command to launch and run a compiled AppleScript (which does not need to be an application, just a script that has been saved as Compiled instead of Plain Text) just as though you had loaded it into the Script Editor and pressed Run. You can also pass parameters to âosascriptâ that will be handed to your script as parameters to the run() handler, just like calling an AppleScript from Xojo.
Another nice thing about this approach is that you can edit the scripts while your app is running and the changes will be executed the next time it is called via âosascriptâ. Thatâs because âosascriptâ reads the script file every time you run it. This is a great debugging feature.
OK, I tacked together a quick demonstration. Hereâs the script:
on run
set looper to 1
repeat
log looper
set looper to looper + 1
delay 1
end repeat
end run
Drop that text into a Script Editor document and save the script as a Script. Then, use the âosascriptâ command to execute it in a Terminal window:
osascript /path/to/script.scpt
The script will run and report 1, 2, 3⊠etc. until you stop it. If you run the âosascriptâ command from an Xojo Shell with Mode=Asynchronous, youâll get the numbers one at a time, with a 1 second delay, in the Shell.DataAvailable event.
This should give you exactly what you need to have your script report ongoing progress. Note that the AppleScript âlogâ command only works when running in the Terminal - you wonât see any output when running in the Script Editor.
Eric, that is brilliant! Absolutely perfect and worked like a charm. I really do appreciate you taking the time to do this. I was patting myself on the back for my little workaround but this is so much more elegant and less prone to error. I love stuff like this. So thank you again.
Eric, thanks for the insights on AppleScript-to-Apple Events conversion. Yes, my AS is pretty long and AE looked beyond me at first glance. So Iâm all over the âosascriptâ command.