Home > Software engineering >  Apple Script: How to pass arguments between apps
Apple Script: How to pass arguments between apps

Time:09-24

I have 2 applescript apps 1st one actually invokes the 2nd app to run.

App 1

on open location this_URL
    tell application "App2" to activate
end open location

App2

# does Something

Now I want to send the variable this_URL from app 1 to app 2 so that it can process it. I am new to applescript and I searched alot but all I got was how to send commands to prebuilt apps. I want to know how can I send the variable and how can I handle that variable in the second app ?

Edit :

Just adding a sample approach what I am doing now but I don't think that this is the best way of doing it.

App1:

on open location this_URL
    set the clipboard to this_URL
    tell application "App2" to activate
end open location

App2:

set this_URL to ( the clipboard as text )

# does Something

set the clipboard to ""

CodePudding user response:

Here is other approach to control App2 from App1 application:

App1:

my open_Location("https://www.google.gr")

on open_Location(this_URL)
    set App2 to load script (alias "HARD_DISK:Users:123:Desktop:App2.app:")
    displayURL(this_URL) of App2
end open_Location

App2 (saved on the desktop as you see the alias above):

on displayURL(theURL)
    activate
    display dialog theURL
end displayURL

NOTE: you can assign in the App2 parameters to on run handler as well. (that is, to any handler of App2). And, you can set in the App1 (get path to application "App2") instead of alias.

set App2 to load script (get path to application "App2")

CodePudding user response:

Assuming App2 is a Script Editor applet, not an Xcode app, you can call its handlers directly.

App 1:

on open location this_URL
    tell application "App2" to do_stuff(this_URL)
end open location

App 2:

on do_stuff(this_URL)
    activate
    display dialog this_URL with title "App2"
end do_stuff

When saving App2 as “Application”, make sure you check the “Stay open after run handler” option, otherwise it will automatically quit before it can handle your calls.

  • Related