text
stringlengths 0
15.7k
| source
stringlengths 6
112
|
---|---|
else {
// open a new tab in an existing iTerm window
app.doShellScript("open -a iTerm") // bring just one iTerm window to front, unminimized if needed
iTerm.currentWindow.createTabWithDefaultProfile()
}
|
Open In iTerm copy 2.applescript
|
sendShellScript(iTerm, params.folderPath)
|
Open In iTerm copy 2.applescript
|
// otherwise we don't need to script iTerm directly at all, "open -a" does what we need
|
Open In iTerm copy 2.applescript
|
app.doShellScript("open -a iTerm " + quotedFormOf(params.folderPath))
|
Open In iTerm copy 2.applescript
|
// ----- utility functions -----
|
Open In iTerm copy 2.applescript
|
// Collects any command-line parameters passed to the application, returning them as an array;
|
Open In iTerm copy 2.applescript
|
// giving run() an 'argv' parameter doesn't seem to work, for whatever reason (on macOS Sierra 10.12.6).
|
Open In iTerm copy 2.applescript
|
//
|
Open In iTerm copy 2.applescript
|
function collectCommandLineParameters() {
// see https://github.com/JXA-Cookbook/JXA-Cookbook/wiki/Shell-and-CLI-Interactions
var argv = []
var args = $.NSProcessInfo.processInfo.arguments // NSArray
for (var i = 0; i < args.count; i++) {
argv.push(ObjC.unwrap(args.objectAtIndex(i)))
}
|
Open In iTerm copy 2.applescript
|
return argv
|
Open In iTerm copy 2.applescript
|
// Displays an alert dialog.
|
Open In iTerm copy 2.applescript
|
// The 'buttons' parameter is optional, and defaults to one OK button.
|
Open In iTerm copy 2.applescript
|
function displayAlert(title, details, buttons) {
if (!buttons) buttons = ["OK"]
return app.displayAlert(title, { message: details, as: "critical", buttons: buttons })
|
Open In iTerm copy 2.applescript
|
// Gets the folder being displayed in Finder's frontmost window.
|
Open In iTerm copy 2.applescript
|
// Returns an empty string if there are no Finder windows (including minimized, full-screen, or in another space);
|
Open In iTerm copy 2.applescript
|
// throws an exception if the frontmost Finder window isn't displaying an actual on-disk folder.
|
Open In iTerm copy 2.applescript
|
function getFinderFolder() {
var finder = Application("Finder")
if (!finder.finderWindows.length) return ""
var win = finder.finderWindows[0]
var type = win.target.class()
try {
if (type == "computer-object") {
return "/Volumes" // closest analogue for "this computer"
}
|
Open In iTerm copy 2.applescript
|
// see https://stackoverflow.com/questions/45426227/get-posix-path-of-active-finder-window-with-jxa-applescript
|
Open In iTerm copy 2.applescript
|
return $.NSURL.alloc.initWithString(win.target.url()).fileSystemRepresentation
|
Open In iTerm copy 2.applescript
|
catch (ex) {
if (type == "trash-object" || (type == "folder" && win.name() == "Trash")) {
// items shown by Finder in the Trash can come from various places (e.g. mounted drives, iCloud Drive),
// so we'll just use whatever macOS says is "the path to Trash" (always ~/.Trash as far as I can tell)
return app.pathTo("trash", { as: "alias", folderCreation: false }).toString()
|
Open In iTerm copy 2.applescript
|
var err = new Error(win.name())
|
Open In iTerm copy 2.applescript
|
err.noFolderFound = true
|
Open In iTerm copy 2.applescript
|
throw err
|
Open In iTerm copy 2.applescript
|
// Gets the full path to our modifier-keys helper program.
|
Open In iTerm copy 2.applescript
|
function getPathToCheckModifierKeys() {
var pathToMe = app.pathTo(this, { as: "alias" }).toString()
|
Open In iTerm copy 2.applescript
|
if (pathToMe.endsWith(".app/")) pathToMe = pathToMe.slice(0, -1)
|
Open In iTerm copy 2.applescript
|
if (pathToMe.endsWith(".app")) {
return pathToMe + "/Contents/Resources/modifier-keys"
}
|
Open In iTerm copy 2.applescript
|
// assume we're running in Script Editor
|
Open In iTerm copy 2.applescript
|
var lastSlash = pathToMe.lastIndexOf('/')
|
Open In iTerm copy 2.applescript
|
return pathToMe.substring(0, lastSlash) + "/modifier-keys/modifier-keys"
|
Open In iTerm copy 2.applescript
|
// Determines whether or not the iTerm application is already running.
|
Open In iTerm copy 2.applescript
|
function iTermIsRunning() {
var iTermProcesses = Application("System Events").processes.whose({ name: { _equals: "iTerm2" }})
|
Open In iTerm copy 2.applescript
|
return iTermProcesses.length > 0
|
Open In iTerm copy 2.applescript
|
// Parses command-line parameters into the given 'params' object (setting 'openTab' and/or 'folderPath'),
|
Open In iTerm copy 2.applescript
|
// ignoring any invalid options, and keeping the last value for conflicting options or folderPath.
|
Open In iTerm copy 2.applescript
|
function parseCommandLineParameters(argv, params) {
var endOfOptions = false
for (var i = 1; i < argv.length; i++) {
var arg = argv[i].trim()
if (arg == "") continue
if (arg == "--") {
endOfOptions = true
continue
}
|
Open In iTerm copy 2.applescript
|
if (!endOfOptions && (arg == "-t" || arg == "--tab")) {
params.openTab = true
}
|
Open In iTerm copy 2.applescript
|
else if (!endOfOptions && (arg == "-w" || arg == "--window")) {
params.openTab = false
}
|
Open In iTerm copy 2.applescript
|
else {
params.folderPath = arg
}
|
Open In iTerm copy 2.applescript
|
// Returns the quoted form of a string, like AppleScript's "quoted form of".
|
Open In iTerm copy 2.applescript
|
// From https://stackoverflow.com/questions/28044758/calling-shell-script-with-javascript-for-automation
|
Open In iTerm copy 2.applescript
|
function quotedFormOf(str) {
return "'" + str.replace(/'/g, "'\\''") + "'"
}
|
Open In iTerm copy 2.applescript
|
// Sends a change-directory shell script to the current iTerm window.
|
Open In iTerm copy 2.applescript
|
function sendShellScript(iTerm, folder) {
if (folder == null || folder == "") return
var shellScript = " cd " + quotedFormOf(folder) + " && clear && printf '\\e[3J'"
// loop for up to ~10s waiting for iTerm to be ready, in case it's just starting up
for (var attempt = 0; attempt < 100; attempt++) {
try {
iTerm.currentWindow.currentSession.write({ text: shellScript })
|
Open In iTerm copy 2.applescript
|
break
|
Open In iTerm copy 2.applescript
|
catch (ex) {
// wait just a bit before trying again
delay(0.1)
}
|
Open In iTerm copy 2.applescript
|
// Detects whether the fn modifier key is down, and decides whether we should open a new iTerm tab this time
|
Open In iTerm copy 2.applescript
|
// (as opposed to a new iTerm window).
|
Open In iTerm copy 2.applescript
|
function shouldOpenTabThisTime() {
var pathToCheckModifierKeys = getPathToCheckModifierKeys()
var modifierKeys = app.doShellScript(quotedFormOf(pathToCheckModifierKeys))
if (modifierKeys.indexOf("option") != -1) {
return null // request early exit
}
|
Open In iTerm copy 2.applescript
|
// open a tab unless the fn or shift key is down
|
Open In iTerm copy 2.applescript
|
return (modifierKeys.indexOf("fn") == -1 && modifierKeys.indexOf("shift") == -1)
|
Open In iTerm copy 2.applescript
|
global myAlbums
|
sort_media.applescript
|
global filedMedia
|
sort_media.applescript
|
global filedMediaIds
|
sort_media.applescript
|
set myAlbums to {}
|
sort_media.applescript
|
set filedMedia to {}
|
sort_media.applescript
|
set filedMediaIds to {}
|
sort_media.applescript
|
activate application "Photos"
|
sort_media.applescript
|
recursiveAlbumCollection(application "Photos", "")
|
sort_media.applescript
|
set collectedFiledTime to current date
|
sort_media.applescript
|
tell application "Photos" to display dialog "Found " & (count of filedMedia) & " media items filed in the " & (count of myAlbums) & " albums below in approx. " & (collectedFiledTime - startTime) & " seconds: " & return & my utils's listToString(myAlbums, return)
|
sort_media.applescript
|
set continueTime to current date
|
sort_media.applescript
|
set filedMediaIdsString to utils's listToString(filedMediaIds, " ")
|
sort_media.applescript
|
tell application "Photos" to set mediaItemIdsList to id of media items
|
sort_media.applescript
|
set mediaItemIdsString to utils's listToString(mediaItemIdsList, " ")
|
sort_media.applescript
|
set unfiledMediaIndicesString to do shell script (POSIX path of ((path to me as text) & "::" & "filter_unfiled_media.sh")) & " \"" & mediaItemIdsString & "\" \"" & filedMediaIdsString & "\""
|
sort_media.applescript
|
set unfiledMediaIndicesList to words of unfiledMediaIndicesString
|
sort_media.applescript
|
set unfiledMedia to getUnfiledMedia(unfiledMediaIndicesList)
|
sort_media.applescript
|
set collectedUnfiledTime to current date
|
sort_media.applescript
|
tell application "Photos" to display dialog "Found " & (count of unfiledMedia) & " (remaining) unfiled media items in approx. " & (collectedUnfiledTime - continueTime) & " seconds."
|
sort_media.applescript
|
createAlbum("Filed Media", filedMedia)
|
sort_media.applescript
|
createAlbum("Unfiled Media", unfiledMedia)
|
sort_media.applescript
|
on recursiveAlbumCollection(parent, albumPath)
|
sort_media.applescript
|
repeat with a in albums
|
sort_media.applescript
|
if "Unfiled Media" is not in name of a and "Filed Media" is not in name of a then
|
sort_media.applescript
|
set end of myAlbums to (albumPath & name of a as string)
|
sort_media.applescript
|
set filedMedia to (filedMedia & media items of a)
|
sort_media.applescript
|
set filedMediaIds to (filedMediaIds & id of media items of a)
|
sort_media.applescript
|
my recursiveAlbumCollection(f, (albumPath & name of f as string) & "/")
|
sort_media.applescript
|
end recursiveAlbumCollection
|
sort_media.applescript
|
on getUnfiledMedia(unfiledMediaIndexList)
|
sort_media.applescript
|
set mediaItemList to media items
|
sort_media.applescript
|
set unfiledMedia to {}
|
sort_media.applescript
|
repeat with i in unfiledMediaIndexList
|
sort_media.applescript
|
set end of unfiledMedia to item i of mediaItemList
|
sort_media.applescript
|
return unfiledMedia
|
sort_media.applescript
|
end getUnfiledMedia
|
sort_media.applescript
|
on getUnfiledMediaDirect()
|
sort_media.applescript
|
set filteredMedia to {}
|
sort_media.applescript
|
repeat with m in media items
|
sort_media.applescript
|
if id of m is not in filedMediaIds then
|
sort_media.applescript
|
set end of filteredMedia to m
|
sort_media.applescript
|
display dialog "Found " & (count of filteredMedia) & " (remaining) unfiled media items in approx. " & (endTime - startTime) & " seconds."
|
sort_media.applescript
|
return filteredMedia
|
sort_media.applescript
|
end getUnfiledMediaDirect
|
sort_media.applescript
|
on createAlbum(albumName, mediaList)
|
sort_media.applescript
|
set albumName to albumName & " - " & short date string of (current date) & ", " & time string of (current date)
|
sort_media.applescript
|
set newAlbum to make new album named albumName
|
sort_media.applescript
|
add mediaList to newAlbum
|
sort_media.applescript
|
display dialog "Album created in approx. " & (endTime - startTime) & " seconds: " & albumName
|
sort_media.applescript
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.