Issue with pre-compiling Applescript in Swift 5

I am using information from the post at https://developer.apple.com/forums/thread/98830

Hi, I am using the example from the above forum post in my Swift app and it's working perfectly however pre-compiling the app is causing me an issue. My script is checking if an app existing using Finder and does this before doing other tasks but for some reason. when my app first runs and the script compiles, it is looking for the app and prompting the user to choose it. Any subsequent script calls work as expected.

    let script = NSAppleScript(source: """
      on lazyPasteRDP()
        try
          tell application "Finder" to get displayed name of application file id "com.microsoft.rdc.macos"
          set appExists to true
        on error
          set appExists to false
        end try
        if appExists
          tell application "System Events"
            set activeApp to name of first application process whose frontmost is true
            if "Microsoft Remote Desktop" is in activeApp then
              tell application "myApplication"
                activate
              end tell
              tell application "Microsoft Remote Desktop"
                activate
                delay 0.5
                tell application "System Events" to keystroke "v" using control down
              end tell
            else
              tell application "System Events" to keystroke "v" using command down
            end if
          end tell
        else
          tell application "System Events" to keystroke "v" using command down
        end if
      end lazyPasteRDP
      """
    )!

Is there a way to prevent prompting the user to choose the app during compile time? Again, once the script is compiled, the logic works as expected and doesn't prompt again. The other option would be to only compile the script on a condition but my feeble attempts haven't worked. I tried to post a comment in the original thread but couldn't get enough detail in the 500 character limit

There’s no specific way to tell NSAppleScript to avoid user interaction when compiling. You can probably do that with the lower-level OSAKit but, honestly, I think that’s the wrong path to take.

I’d tackle this by avoid the “choose an app” prompt entirely. The best way to do that is to:

  1. Ship a pre-compiled script.

  2. Avoid referencing apps by name.

With regards the latter, you’re not using an terminology from the Microsoft Remote Desktop app, so you can target the process dynamically. For example:

tell application "System Events"
    set te to application process "TextEdit"
end tell
tell te
    activate
end tell

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

At first, execute other meaningless AppleScript to control other applications.

tell application "Microsoft Remote Desktop" to activate
tell me activate

Once, application control and authentication executed, your app is authenticated. You can detect tcc status by using Objective-C program.

I built it as a Cocoa framework and call it from AppleScript application.

http://piyocast.com/as/archives/4819

Issue with pre-compiling Applescript in Swift 5
 
 
Q