Skip to content

Instantly share code, notes, and snippets.

@cgiffard
Created April 26, 2014 12:51
Show Gist options
  • Save cgiffard/11319369 to your computer and use it in GitHub Desktop.
Save cgiffard/11319369 to your computer and use it in GitHub Desktop.
Nasty hack to get Safari Tabs In Node
var applescript = require("applescript");
var getTabs = ' \n\
tell application "Safari" \n\
set urllist to URL of every tab of every window \n\
set namelist to name of every tab of every window \n\
\n\
set theDoc to "" as string \n\
set windowcount to length of urllist \n\
\n\
repeat with cWindow from 1 to windowcount \n\
set ctabList to item cWindow of urllist \n\
set ctabCount to length of ctabList \n\
\n\
repeat with cTab from 1 to ctabCount \n\
set cUrl to item cTab of ctabList \n\
set cName to item cTab of item cWindow of namelist \n\
\n\
set theDoc to theDoc & cName & "₮" & cUrl & linefeed \n\
end repeat \n\
end repeat \n\
return theDoc \n\
end tell ';
module.exports = function(cb) {
applescript.execString(getTabs, function(err, data) {
if (err) return cb(err);
var records =
data.split(/\n+/)
.filter(function(row) {
return row.indexOf("₮") >= 0;
})
.map(function(data) {
return {
"name" : data.split("₮").shift(),
"url" : data.split("₮").pop()
}
});
cb(null, records);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment