AppleScript unable to use System Events when ran using URL protocol

35 Views Asked by At

I have an AppleScript file with following codes, that suppose to do three things -

  1. Read from URL
  2. Write the value to some place as text file
  3. Activate an application if it's already running

Here's the code that I have:

#!/bin/bash
on open location this_URL
    set userHomePath to POSIX path of (path to home folder)
    set argFile to userHomePath & "Downloads/uriNotifier.txt"
    do shell script "echo  " & quoted form of this_URL & " >  " & quoted form of argFile
end open location

set processIsRunning to true
tell application "System Events"
    set runningProcesses to processes whose bundle identifier is "my.App.ID"
end tell
if runningProcesses is {} then set processIsRunning to false
if processIsRunning is true then
    do shell script "open -b \"my.App.ID\""
end if

I export the file as Application, and modified its Info.plist file to enable URL protocol:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleIdentifier</key>
        <string>com.apple.ScriptEditor.id.SendToMyApplication</string>
        <key>CFBundleTypeRole</key>
        <string>Viewer</string>
        <key>CFBundleURLName</key>
        <string>com.apple.ScriptEditor.id.SendToMyApplication</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>sendtomyapplication</string>
        </array>
    </dict>
</array>
<key>NSAppleEventsUsageDescription</key>
<string>Sending for my application.</string>

Now when I ran the protocol on a browser using sendtomyapplication://someValue it does following things:

  1. It triggers the application I exported with AppleScript successfully
  2. It writes the URL value to a text file under Users/$user/Downloads/uriNotifier.txt
  3. It fails to activate the application with ID my.App.ID

However, if I ran the AppleScript-application by double-click, or from Terminal, it successfully able to activate the application with ID my.App.ID.

Can someone please point me what I'm doing wrong that when it is running from URL, the targeted application is not activating (?) Thanks!

0

There are 0 best solutions below