-
-
Save anonymous/a23e3a251d4e94c55ce8 to your computer and use it in GitHub Desktop.
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
(* | |
_Integration of JDownloader in Safari (Mac OS X)_ | |
(AddLinksToJDownloader.scpt created by a jduser) | |
This apple script is for usage in Automator to create a service, which then can be used in Safari via | |
context menu or shortcut to download selected links in JDownloader. | |
Therefore it sends requests to the jd-http-remote. See more info via localhost:10025/help when | |
jd-remote is activated. | |
Also very nice is setting a Keyboard-shortcut in Keyboard.prefPane for App Safari. Simply add there a | |
shortcut with the name of the service and set the key-command to e.g. CMD+F2, now it feels like | |
the Flash-Got plugin in Firefox. | |
Inspiration came from: | |
http://elasticthreads.tumblr.com/post/196448351/snow-leopard-services-pt1-shortening | |
Todo: | |
- also implement text links extraction like we see it in the elasticthreads urlshortening | |
script (usage of the input inTxt) | |
- seperate link extraction from later usage, so other custom scripts can be run on a list of extracted | |
links (atm the final shell script is build during link extraction) | |
- the jd-remote is a bit unfinished imho: | |
1. adding multiple links with a single request does not work, so atm we have to do one remote | |
call for each link | |
2. adding links like: http://somelinksite.com/download/?http://share-links.biz/_foobarommelbommel | |
does not work correctly. Atm we try to call the remote with the second part of such a | |
link (second http://...). It is a hacky workaround! | |
- if Safari losts focus during invoke of the script, it seems not to work correctly | |
*) | |
global AppIcon | |
global JDRemoteCmdUrl | |
global PieceOfSuccessSummaryMsg | |
global ShowLinksInGrowlMsg | |
on run {inTxt} | |
set JDRemoteCmdUrl to "localhost:10025/action/add/links/grabber1/start1/" | |
set ShowLinksInGrowlMsg to true | |
set AppIcon to "Safari" | |
set PieceOfSuccessSummaryMsg to " been submitted to JDownloader." | |
set shellScriptStr to getTheLinksAndBuildShellScript() | |
set resultStr to runShellScript(shellScriptStr) | |
sendResultToGrowl(resultStr) | |
end run | |
on runShellScript(shQ) | |
if shQ is not "" then | |
try | |
set resultStr to do shell script shQ | |
on error | |
set resultStr to "" | |
end try | |
else | |
set resultStr to "" | |
end if | |
return resultStr | |
end runShellScript | |
on sendResultToGrowl(resultStr) | |
if not first character of resultStr is "0" and resultStr contains PieceOfSuccessSummaryMsg then | |
triggerGrowl(resultStr, "Success") | |
else | |
if resultStr is not "" then | |
set resultStr to return & return & resultStr | |
end if | |
triggerGrowl("Something went wrong." & resultStr, "Fail") | |
end if | |
end sendResultToGrowl | |
on getTheLinksAndBuildShellScript() | |
set theScriptStr to "" | |
tell application "Safari" | |
set theScriptStr to do JavaScript " | |
function getLinksAndBuildScript() { | |
if (window.console) console.log('getLinksAndBuildScript() - begin'); | |
var selection = window.getSelection(); | |
var SQ = \"'\"; // singleQuote | |
var finalScriptStr = ''; | |
var count = 0; | |
for (var i = 0 ; i < document.links.length ; i++) { | |
var linkNode = document.links[i]; | |
if (selection.containsNode(linkNode, true) && linkNode.href) { | |
//next line is a hack, the bug with urls that have urls as parameter should be fixed in JD | |
var fixedUrl=linkNode.href.replace(/.*http:\\/\\//, 'http://'); | |
var cmd = 'curl --silent --show-error --stderr - ' + | |
SQ + '" & JDRemoteCmdUrl & "' + encodeURI(fixedUrl) + SQ; | |
//cmd += ' > /dev/null 2>&1'; | |
finalScriptStr = finalScriptStr + cmd + ' && '; | |
count++; | |
} | |
} | |
var extractLinksSubtitutions = 's/.*Link\\\\(s\\\\) added. \\\\(.*//g; '; | |
if (" & ShowLinksInGrowlMsg & ") { | |
extractLinksSubtitutions = 's/\\\\)?Link\\\\(s\\\\) added. \\\\(/\\\\n\\\\n/g; s/ *\\\\)$//; '; | |
} | |
var badCharsSubtitutions = 's/\"//g; s/\\\\[/{/g; s/\\\\]/}/g; s/\\\\r//g; '; | |
var perlFilter=' | perl -ne ' + SQ + badCharsSubtitutions + extractLinksSubtitutions + 'print;' + SQ; | |
var grammarSnipped = ' has'; | |
if (count != 1) { grammarSnipped = 's have' ; } | |
var summaryMsg = count + ' link' + grammarSnipped + '" & PieceOfSuccessSummaryMsg & "'; | |
finalScriptStr = 'response=\"`' + finalScriptStr + ' true`\"' + | |
' && ' + | |
' echo ' + SQ + summaryMsg + SQ + | |
' ; ' + | |
'eval echo ' + SQ + '$response' + SQ + perlFilter; | |
if (window.console) console.log('getLinksAndBuildScript() - end, result: ' + finalScriptStr); | |
return finalScriptStr; | |
} | |
getLinksAndBuildScript()" in document 1 | |
end tell | |
return theScriptStr | |
end getTheLinksAndBuildShellScript | |
on triggerGrowl(currentStatus, currentTitle) | |
try | |
tell application "System Events" | |
set isGrlRunning to (count of (every process whose name is "GrowlHelperApp")) > 0 | |
end tell | |
ignoring application responses | |
if isGrlRunning then | |
set osaSc to "tell application \"GrowlHelperApp\" | |
set the allNotificationsList to {\"Notification\"} | |
set the enabledNotificationsList to {\"Notification\"} | |
register as application \"Safari2JD\" all notifications allNotificationsList default notifications enabledNotificationsList icon of application \"" & AppIcon & "\" | |
notify with name \"Notification\" title \"" & currentTitle & "\" description \"" & currentStatus & "\" application name \"Safari2JD\" | |
end tell" | |
set shSc to "osascript -e " & quoted form of osaSc & " > /dev/null 2>&1 &" | |
ignoring application responses | |
do shell script shSc | |
end ignoring | |
end if | |
end ignoring | |
end try | |
end triggerGrowl |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment