1

I have found a few things on how to run another applescript application but not on giving it an input. On applescript site I found this:

tell application "NonStayOpen"
  launch
  stringTest("Some example text.")
end tell

Which would be a solution, I just rap the script in a handler and pass my variable. But not matter what I do I can not get the handler to activate.

Ideal Situation run script that contains the following:

tell application id "com.apple.testhandlerApp"
  TestHandler("Hi") --Or if I can get the Open(SomeVar) to work
end tell

which then activates application testhandlerApp containing the script:

on TestHandler(somevar)
    set contentText to somevar as string
    display dialog contentText
end TestHandler

Giving me a dialog saying "Hi". Reason being is I want to put the first bit of code into automator to runs a complex application that takes a text input. Right now all I get is "Connection is invalid." If I didn't need the input it seems using activate works fine.

2 Answers 2

4

There's 2 ways I can think of.

1) Create your "testhandlerApp.app" with the following code. I saved mine on the desktop. You'll notice it has an "on run" handler that accepts an argument list which then runs TestHandler(). The try block of code just prevents an error if you launch the app yourself. Obviously in that case there is no argument list passed and the code errors so the try block prevents that.

NOTE: this method will also work if you save this as a plain script instead of an application.

on run argList
    try
        class of argList
    on error
        set argList to {"No arguments were passed!"}
    end try

    TestHandler(item 1 of argList)
end run

on TestHandler(somevar)
    set contentText to somevar as string
    display dialog contentText
end TestHandler

Then to call this from another applescript you use the "run script" command as follows.

set appPath to (path to desktop as text) & "testhandlerApp.app"
run script file appPath with parameters {"Hi"}

You can even call it from the command line with something like this...

osascript /path/to/script arg1 arg2

2) Create your "testhandlerApp.app" with the following code. Save this as a "stay open" applescript application by checking the "stay open" checkbox in the save window.

on run
end run

on TestHandler(somevar)
    set contentText to somevar as string
    display dialog contentText
end TestHandler

Then to call this from another applescript use this...

tell application "testhandlerApp"
    TestHandler("Hi")
    quit
end tell

Good luck!

Sign up to request clarification or add additional context in comments.

2 Comments

#1 worked like a charm. I knew it was either on open or on run but the parameters was where I went brain dead. Works for me. Have a green checkmark :)
For the record i had to access one param so i did something like on run arg set message to item 1 of arg
0

This link for AppleScript Language Guide might help. I haven't done what you are asking before- as I am pretty new to AppleScript, but it appears Automator as an on run function that may help. Checkout this link of AppleScript tips -including the on run function.

1 Comment

Automator is the starting point that will pass a variable to a standalone applescript app that uses an "On Open" term with an input. I should also be able to use two standalone apps and have one tell the other what to do. Automator I want to say is moot point but want to add in my final intent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.