• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Applescript URL handler - CLI "open" command not calling "on open location"
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Applescript URL handler - CLI "open" command not calling "on open location"


  • Subject: Applescript URL handler - CLI "open" command not calling "on open location"
  • From: Joshua Ochs <email@hidden>
  • Date: Mon, 25 Feb 2013 14:20:38 -0600

Greetings, all. I'm working on a fairly simple one-trick-pony to intercept magnet links, then pass them on with the proper options to keep the BitTorrent client from being activated - which is rather annoying when you're watching stuff on a home theater Mac Mini. (Background: the programs sending such links really should be using "openURLs" with "NSWorkspaceLaunchWithoutActivation", but they're not - so I'm trying to kludge around it using the command line "open -g").

Current flow, which brings BitTorrent client to the foreground:
Program -> openURL("magnet:...") -> LaunchServices -> BitTorrent client
CLI -> open "magnet:..." -> LaunchServices -> BitTorrent client

The following script works well and handles magnet URLs being sent by other programs (e.g. Safari, etc), but it fails when the command-line "open" command is used to send a link! When sent by "open", the "on run" routine fires instead of "on open location" - which means I don't get the link. Rather annoying.

With the following script as the handler for magnet links (BitTorrent client stays in the background):
Program -> openURL("magnet:...") -> LaunchServices -> AppleScript -> on open location handler -> open -g -a -> BitTorrent client
CLI -> open "magnet:..." -> LaunchServices -> AppleScript -> on run handler -> *nada*

 I also wondered if somehow the URL was being passed as an argument to the run handler. Tried it; no dice - empty list. Same if I added an "on open" handler. Even so, it should by all rights be sending it through the same Apple Event system as any other program.

Does anyone see anything wrong here, or have I uncovered a minor-yet-annoying OS bug?

----
To set this up, you need to do three things:
1) Compile the script below as a standalone application.
2) Add the proper lines to the Info.plist, which tells the OS it can accept "magnet" links.
3) Instruct LaunchServices to use this app for magnet links. So far, I've relied on "RCDefaultApp" to set this, although I'd love it if I could build that bit of functionality into the script.

----
Script:

property urlPrefix : "magnet:?"
property clientLocation : ""
property firstRun : true

on run
   configureHandler()
end run

on configureHandler()
   activate
   set clientLocation to (choose file with prompt "Please select the BitTorrent application that should handle magnet links." default location (path to applications folder as alias) of type "APPL" without invisibles) as string
   set firstRun to false
end configureHandler

on open location theURL
   if (the offset of urlPrefix in theURL) < 0 then return
   if firstRun then configureHandler()
   try
       set theCommand to "open -g -a \"" & POSIX path of clientLocation & "\" " & theURL
       do shell script theCommand
   on error
       tell me to display alert "The magnet link could not be processed." message "There was a problem sending the marget link. Usually this is due to a missing BitTorrent application or a bad magnet link. " as critical buttons {"OK"} default button "OK"
   end try
end open location

----
Info.plist additions:

<key>CFBundleIdentifier</key>
<string>com.joshuaochs.silentmagnet</string>

<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>Magnet Link</string>
<key>CFBundleURLSchemes</key>
<array>
<string>magnet</string>
</array>
</dict>
</array>
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Prev by Date: Re: Mail GUI script error
  • Next by Date: Re: Mail GUI script error
  • Previous by thread: Re: Simple apple script to open a message in Mail?
  • Next by thread: Re: Mail GUI script error (koenig.yvan)
  • Index(es):
    • Date
    • Thread