Skip to content

Instantly share code, notes, and snippets.

@glyphobet
Forked from DamnedFacts/MagnetURLHandler.scpt
Last active August 29, 2015 14:17
Show Gist options
  • Save glyphobet/23383a5a5d3783274111 to your computer and use it in GitHub Desktop.
Save glyphobet/23383a5a5d3783274111 to your computer and use it in GitHub Desktop.
(*
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
-- Seems necessary to use ${HOME} instead of just ~
property watch_dir : "${HOME}/Downloads/"
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
set magnet_script to "
magnet_URL='" & magnet_URL & "'
[[ \"$magnet_URL\" =~ xt=urn:btih:([^&/]+)\\&dn=([^&/]+) ]]
echo \"d10:magnet-uri${#magnet_URL}:${magnet_URL}e\" > \"" & watch_dir & "${BASH_REMATCH[2]}.magnet.torrent\"
"
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