Applescript

Hi All,

I’m running applescript no problem by saving as a script, copying into the build and referencing the script in an action. Works great. I wondered if there is anyway to run the script by say storing it in the action itself. I’d like the app to compile without storing the script in the resources folder so it can’t be accessed in the build package if that makes sense ?

Thanks in advance

Conversation from last week that might help you: https://forum.xojo.com/10609-proper-way-to-execute-an-applescript/
Also it should be possible with the clever use of shells and osascript.

make a constant (string)

osascript -e ‘tell application “Mail” to activate’

(replace tell application “Mail” to activate with your applescript code)

dim sh as new shell sh.Execute myscript // your constant name

That method won’t work for a multiline applescipt since everything has to be properly escaped.

it works wth multiline , you must type ’ on begin and end

osascript -e ‘script text’

Here is an example project that will execute an applescript saved as a constant in your app (or from a text area as in my example). Just click the button it will execute the script in the text area. If you need parameter, look at the link I posted earlier and add those to the shell command.

[quote=79117:@Axel Schneider]it works wth multiline , you must type ’ on begin and end

osascript -e ‘script text’[/quote]
Try making “script text” contain an apostrophe like:

display dialog "Something's wrong!" buttons {"OK", "Cancel"} default button 1

/usr/bin/osascript <<-EOF display dialog "Something's wrong!" buttons {"OK", "Cancel"} default button 1 display dialog "It works" buttons {"OK", "Cancel"} default button 1 EOF

Example

or use a single line text with -e ’ on the beginning of every applescript line and ’ on the end of every applescript line

/usr/bin/osascript -e 'tell application "System Events" to activate' -e 'tell application "System Events" to return (choose from list {"Liste1", "Liste2"} with prompt "Choose a Project Type:" with title "Project Type Chooser" OK button name "Select" cancel button name "Quit")'

If you use MBS Plugins, you could use AppleScriptMBS class or NSAppleScriptMBS class.

You can also use osascript to read from a file or, probably, the console standard input.

wow……thanks all for the info. Loads to learn! I ended up going with Jasons example and modify the text area to be a constant as a string ( which in itself was a lightbulb moment so thanks) Seems to work fantastically for what I need. I have noticed though that if I put in a script that requires a dialog box,even something as simple as :

display dialog “hello” buttons “Okay” default button “Okay”

Then all i get in the dialog window is the number 1 . Not what I was expecting. Any ideas why ?

[quote=79204:@damon iddins]I have noticed though that if I put in a script that requires a dialog box,even something as simple as :

display dialog “hello” buttons “Okay” default button “Okay”

Then all i get in the dialog window is the number 1 . Not what I was expecting. Any ideas why ?[/quote]
Ah, yes - let me fix that. The 1 is the error code from the shell command. I’ll have a new version posted in just a minute.

Updated project - this one should work as desired. Make sure the kScriptRunner constant is included or it won’t work if you copy the method to a new project.

Thanks very much everyone . And thanks for taking the extra time Jason.Much appreciated.