Last active
August 15, 2019 21:01
-
-
Save cicloid/69ae6e6c9a6c39d79397 to your computer and use it in GitHub Desktop.
Save all Safari Windows to a Markdown file with timestamp
This file contains hidden or 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
#!/usr/bin/osascript -l JavaScript | |
ObjC.import('Cocoa'); | |
function journalName() { | |
var currentDate = new Date(), | |
cMonth = pad(currentDate.getMonth() + 1, 2), | |
cYear = currentDate.getFullYear(), | |
cDay = pad(currentDate.getDate(), 2), | |
cHours = pad(currentDate.getHours(), 2), | |
cMinutes = pad(currentDate.getMinutes(), 2); | |
return "" + cYear + "-" + cMonth + "-" + cDay + "-" + cHours + cMinutes | |
} | |
function pad(value, length) { | |
return (value.toString().length < length) ? pad("0" + value, length):value; | |
} | |
function getUpfrontPages(header) { | |
var appSafari = Application('Safari'), | |
windowsLst = appSafari.windows, | |
windowsLength = appSafari.windows.length, | |
text = "# " + header + "\n\r"; | |
for (x=0; x < windowsLength; x++) { | |
try { | |
var lstTabs = windowsLst[x].tabs, | |
tabsLength = windowsLst[x].tabs.length; | |
text += "## Window " + (x) + "\n\r"; | |
for (i=0; i< tabsLength; i++) { | |
text += "["+lstTabs[i].name()+"]"; | |
text += "("; | |
text += lstTabs[i].url(); | |
text += ")"; | |
text += "\n\r"; | |
text += "\n\r"; | |
} | |
} catch (e) { | |
console.log(e); | |
}; | |
} | |
return text | |
} | |
var timestamp = journalName(); | |
pagesList = getUpfrontPages(timestamp), | |
str = $.NSString.alloc.initWithUTF8String(pagesList); | |
filename = "~/Dropbox/Pages/" + journalName() + ".md" | |
str.writeToFileAtomically(filename, true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment