Created
May 15, 2013 20:56
-
-
Save DamnedFacts/5587308 to your computer and use it in GitHub Desktop.
This AppleScript runs a handler script that responds to the "magnet:" URL. It will read in the passed URL, execute a shell script to parse the relevant content and save a torrent file into a directory specified by watch_dir property. The variable magnet_script is a terse embedded bash script that quickly parses the URL and writes the torrent fil…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* | |
Copyright (c) 2013, Richard Emile Sarkis <rich@sarkis.info> | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without | |
modification, are permitted provided that the following conditions are met: | |
1. Redistributions of source code must retain the above copyright notice, this | |
list of conditions and the following disclaimer. | |
2. Redistributions in binary form must reproduce the above copyright notice, | |
this list of conditions and the following disclaimer in the documentation | |
and/or other materials provided with the distribution. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | |
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
The views and conclusions contained in the software and documentation are those | |
of the authors and should not be interpreted as representing official policies, | |
either expressed or implied, of the FreeBSD Project. | |
*) | |
(* | |
This AppleScript runs an "on open location" handler that responds to the "magnet:" URL. It will read in the passed URL, execute a shell script to parse the relevant content and save a torrent file into a directory specified by watch_dir property. The variable magnet_script is a terse embedded Bash script that quickly parses the URL and writes the torrent file to disk. | |
The "on run" handler configures the applet's plist file to respond to the "magnet:" URL scheme. Just run the applet and click "OK". No manual editing necessary. | |
I suppose there are a number of ways to do this differently, including not embedding a obfuscating Bash script - but hey, it works for me (especially for version 1)! | |
Note: Make sure to save the script as an application. | |
*) | |
-- Set the watch directory for saving our generated torrent files | |
property watch_dir : "~/Dropbox/Torrent/Watch/" | |
property plist_key_name : "CFBundleURLTypes" | |
property plist_key_value : "( {\"CFBundleURLName\" = Magnet;}, {\"CFBundleURLSchemes\" = (magnet);} )" | |
on run | |
set answer to button returned of (display dialog "Set the key/values in Info.plist for URL handling?" buttons {"Quit", "OK"} default button 1) | |
if answer is equal to "OK" then | |
do shell script "defaults write '" & (POSIX path of (path to me)) & "Contents/Info.plist" & "' " & plist_key_name & " '" & plist_key_value & "'" | |
display dialog "Updated Plist file to handle magnet URL scheme." | |
quit | |
end if | |
end run | |
on open location magnet_URL | |
-- The shell script to process the magnet URL | |
set magnet_script to " | |
MAGURL=\"" & magnet_URL & "\" | |
cd " & watch_dir & " | |
[[ \"$MAGURL\" =~ xt=urn:btih:([^&/]+) ]] || echo fail | |
[[ \"$MAGURL\" =~ dn=([^&]+) ]] || exit | |
echo \"d10:magnet-uri${#MAGURL}:${MAGURL}e\" > " & watch_dir & "/\"${BASH_REMATCH[1]}.torrent\" | |
" | |
-- Execute the shell script | |
do shell script magnet_script | |
end open location |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment