0

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 ""
Shubham Kumar
  • 538
  • 5
  • 12
  • I don't really understand what your goal is? Do you want to use the variable in the other app? Just use `this_URL` there. Want to read from an app, save it as a variable and then use that variable in another app? If it's a browser, you can execute javascript in there (which I see you are quite familiar with) using either `execute javascript "code goes here"` in Chrome/Chromium-based browsers or `do javascript "code goes here"` in safari. Be sure to target the right tab with the `tell` statement though. By this I mean: Please provide more detail, in this current state we are unable to help. – gurkensaas Sep 21 '21 at 18:02
  • @gurkensaas Its exactly "Want to read from an app, save it as a variable and then use that variable in another app?" but I am developing both the apps. So I just need a way to transfer this variable to the other app and handle it from there. – Shubham Kumar Sep 21 '21 at 18:21
  • You should probably trigger them using `do shell script "osascript /path/to/your/applescript.app"` and then see what they return. You could alternatively make the apps [write to file](https://stackoverflow.com/questions/67728387/) and read from file using `read file ("/path/to/your/file.txt" as POSIX file)`. – gurkensaas Sep 21 '21 at 18:27
  • @gurkensaas I have edited the question with a sample approach/workaround I am using. It may help you understanding problem – Shubham Kumar Sep 21 '21 at 18:28
  • @gurkensaas Is there a way I can `tell app2 do someting(this_url) end tell` and handle this `something` in my app2 – Shubham Kumar Sep 21 '21 at 18:35
  • *but I am developing both the apps*. Then you can add AppleScript support by providing a sdef file and the appropriate logic. However this is not trivial, so the clipboard workaround is the easiest way. – vadian Sep 21 '21 at 18:56
  • @vadian can you please give me some more detail regarding the sdef file and how can it be implemented – Shubham Kumar Sep 21 '21 at 19:19
  • Please search for Cocoa Scripting. The subject is pretty complicated. – vadian Sep 21 '21 at 20:05
  • @vadian: Cocoa Scripting is only needed when adding full AppleScript support to Cocoa apps developed in Xcode. It is not needed for apps saved from Script Editor, since SE applets already have basic Apple event handling support built in. – foo Sep 23 '21 at 09:14
  • In another comment you mention using the second app to interact with the Terminal. As mentioned in two of the answers so far, App1 has direct access to the handlers of App2 - since this is starting to look like an X-Y problem, can you clarify what App2 is doing? – red_menace Sep 24 '21 at 17:32

3 Answers3

1

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")
Robert Kniazidis
  • 1,760
  • 1
  • 7
  • 8
1

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.

foo
  • 664
  • 1
  • 4
  • 4
  • Hi have you tested this? because I tried to implement this but it didn't worked. Log : App2 was opened but no dialog box appeared. – Shubham Kumar Sep 27 '21 at 13:30
  • My Steps : I saved both files as txt. Then compiled them with `osacompile` also added `-s` stay open option for app2 thus made `app1.app` and `app2.app`. Then edited the plist of app1 for handling urls. Then executed app1 with the help of url. – Shubham Kumar Sep 27 '21 at 13:33
  • Works here. I suggest you try calling app2 directly from Script Editor to confirm it is working correctly: `tell application "app2" to do_stuff("TEST")`. If that part works, add a temporary `display dialog` command to app1’s `open location` handler to confirm it is handling URLs correctly. – foo Sep 28 '21 at 08:20
0

I've saved a text file to the tmp folder. You can use multiple lines or other delimiters for additional variables:

-- program 1
set myFilePath to "Macintosh HD:tmp:Variable.txt"
set wrongAnswer to "54"
set theAnswer to "42"
set myFile to open for access myFilePath with write permission
set eof of the myFile to 0
write wrongAnswer & return to myFile
write theAnswer to myFile
close access myFile


--program 2
set myFilePath to "Macintosh HD:tmp:Variable.txt"
set the_list to paragraphs of (read file myFilePath)
repeat with curRow in the_list
    display dialog curRow
end repeat
kaoskev
  • 31
  • 3