Skip to content

Commit f7f42f1

Browse files
committed
Replace lstool for swift script.
Replaces lstool for a brief swift command-line tool. As much as lstool will be missed, this is enough for the purposes of this plug-in.
1 parent 6e81b2d commit f7f42f1

File tree

6 files changed

+38
-12
lines changed

6 files changed

+38
-12
lines changed

Applescript_Code.txt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,24 @@ on open location this_URL
2727
end open location
2828
tell application "Finder" to set thisPath to (POSIX path of (application file id "org.deluge.MagnetURIHandler" as string))
2929
log thisPath
30-
set lsToolRead to do shell script (quoted form of (thisPath & "/Contents/Resources/lstool") & " read url magnet")
31-
if (lsToolRead contains "Magnet Handler.app") then
30+
set lsToolRead to do shell script (quoted form of (thisPath & "/Contents/Resources/registerHandler") & " -check")
31+
log lsToolRead
32+
if (lsToolRead contains "org.deluge.magneturihandler") then
3233
display dialog "Magnet Handler is already the default application to handle magnet URIs" buttons {"OK"} with title "Notification" with icon POSIX file (thisPath & "/Contents/Resources/deluge_magnet.icns")
3334
else
3435
try
3536
set dialogResult to display dialog "Magnet Handler has not been configured to handle magnet URIs.
3637

3738
Would you like to do this now?" buttons {"Yes", "No"} default button "Yes" cancel button "No" with title "Notification" with icon POSIX file (thisPath & "/Contents/Resources/deluge_magnet.icns")
3839
on error number -128
40+
return
3941
end try
4042
try
43+
on error number -128
44+
return
4145
if button returned of dialogResult is "Yes" then
42-
do shell script (quoted form of (thisPath & "/Contents/Resources/lstool") & " write url magnet org.deluge.MagnetURIHandler")
46+
do shell script (quoted form of (thisPath & "/Contents/Resources/registerHandler") & " -register")
4347
display dialog "Magnet Handler has now been configured as the default application to handle magnet URIs" buttons {"OK"} with title "Notification" with icon POSIX file (thisPath & "/Contents/Resources/deluge_magnet.icns")
44-
4548
end if
4649
end try
4750
end if
-110 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf340
1+
{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470
22
{\fonttbl}
33
{\colortbl;\red255\green255\blue255;}
44
}
-296 KB
Binary file not shown.
49.1 KB
Binary file not shown.

Magnet Handler.app/Contents/Resources/registerHandler.swift

100755100644
Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,35 @@
33
import Foundation
44
import Cocoa
55

6-
let currentHandler = LSCopyDefaultHandlerForURLScheme("magnet" as CFString)!.takeUnretainedValue() as String;
6+
func getArgument() -> String? {
7+
if (2 > CommandLine.arguments.count) {
8+
return nil;
9+
}
10+
else {
11+
return CommandLine.arguments[1];
12+
}
13+
}
14+
15+
let command = getArgument() ?? "null";
16+
17+
switch command {
18+
19+
case "-register":
20+
let currentHandler = LSCopyDefaultHandlerForURLScheme("magnet" as CFString)!.takeUnretainedValue() as String;
21+
22+
if ("org.deluge.magneturihandler" != currentHandler) {
23+
LSSetDefaultHandlerForURLScheme("magnet" as CFString, "org.deluge.magneturihandler" as CFString);
24+
}
25+
else {
26+
print("MagnetURIHandler is already registered to handle magnets.");
27+
}
28+
29+
case "-check":
30+
31+
let currentHandler = LSCopyDefaultHandlerForURLScheme("magnet" as CFString)!.takeUnretainedValue() as String;
32+
print(currentHandler);
33+
34+
default:
35+
print ("Usage: magnetHandler <command>, where <command> is one of:\n\n\t-register\t\t\t (Register MagnetURIHandler as the default handler for magnet:// URIs.)\n\t-check\t\t\t\t (Prints the bundle id for the application currently registered to handle magnet URIs)\n\nmagnetHandler with ether an invalid <command> or without arguments will print this message.");
736

8-
if ("org.deluge.magneturihandler" != currentHandler) {
9-
print("MagnetURIHandler is not registered to handle magnets; fixing that now.");
10-
LSSetDefaultHandlerForURLScheme("magnet" as CFString, "org.deluge.magneturihandler" as CFString);
1137
}
12-
else {
13-
print("MagnetURIHandler is already registered to handle magnets.");
14-
}

0 commit comments

Comments
 (0)