Skip to content

Instantly share code, notes, and snippets.

@ChristoferK
ChristoferK / Chromium#Close Other Tabs.applescript
Last active May 11, 2023 09:17
[Close Every Other Tab In All Browser Windows] A pair of scripts that close every other tab in all windows of a Chromium-based browser or Safari, leaving the currently active tab open by itself. #AppleScript #browser #Chromium #Chrome #Brave #Safari #close #tabs
--OR: tell app id "com.google.Chrome"
tell application id "com.brave.browser" to if it is ¬
running then tell every window to if (exists) ¬
then close (the tabs whose id ≠ my window 1's ¬
active tab's id)
@ChristoferK
ChristoferK / Centre Window.applescript
Last active November 3, 2022 06:34
[Centre Window on Screen] Centres the frontmost window on screen #AppleScript #SystemEvents #UI #window #position
use application "System Events"
property process : a reference to (first process whose frontmost = true)
property window : a reference to front window of my process
property display : a reference to scroll area 1 of process "Finder"
if not (my window exists) then return
set [width, height] to size of my window
set [screenX, screenY] to size of my display
@ChristoferK
ChristoferK / Safari#Filter Tabs By URL.applescript
Created October 13, 2020 15:27
[Match Safari Tabs Against URL Substring] A fast, efficient tab filter that operates over all tabs in all Safari windows to return a reference to those where the URL contains a given phrase. #AppleScript #Safari #browser #windows #tabs #URL #filter #search
on SafariTabsWhoseURLsContain:(phrase as text)
set text item delimiters to linefeed
tell application id "com.apple.Safari" to if it ¬
is running then tell every window to if ¬
it exists then tell (it where (index of ¬
tab 1 where phrase is in the URL) is in ¬
index of tabs) to tell (every tab whose ¬
URL contains the phrase) to if (exists) ¬
then return a reference to it
end SafariTabsWhoseURLsContain:
@ChristoferK
ChristoferK / Create Machine Device Icon (High Sierra).applescript
Last active March 7, 2021 21:57
[Machine Device Icon] Returns a path to a PNG image file containing a hi-resolution representation of the user's machine #AppleScript #AppleScriptObjC #device #computer #machine #icon #NSWorkspace #PNG
use framework "Foundation"
use framework "AppKit"
use scripting additions
property this : a reference to the current application
property nil : a reference to missing value
property _1 : a reference to reference
property NSWorkspace : a reference to NSWorkspace of this
@ChristoferK
ChristoferK / Scriptable Applications.applescript
Last active March 7, 2021 21:56
[Scriptable Apps] Retrieves a list of bundle identifiers for the applications in the /Applications folder (and sub-folders) that are Applescriptable. #AppleScript #applications #scriptable #Finder
property dir : path to applications folder
use Finder : application id "com.apple.Finder"
property contents : a reference to (folder dir)'s entire contents
property list : a reference to application files in its contents
script
property parent : my list
property id : my id
property key : my has scripting terminology
@ChristoferK
ChristoferK / Convert AppleScript Object to String.applescript
Last active April 9, 2020 11:38
[AppleScript Type Coercions] Forced class coercions in AppleScript that circumvent inherent limitations #AppleScript #classes #coercion #text #strings #records #error_handling #text_item_delimiters #null #boolean #true #false #yes #no
to __string__(object)
local object
try
null object
on error E --> "Can’t get null %object%."
E's text 16 thru -2
end try
end __string__
on |$|:value
@ChristoferK
ChristoferK / Script Editor#Default Selected Tab.applescript
Last active January 9, 2020 15:14
[Script Editor#Default Selected Tab] Sets Script Editor's `Replies` pane as the default selected tab #AppleScript #defaults #ScriptEditor #SystemEvents #propertylist
set fp to "~/Library/Preferences/com.apple.ScriptEditor2.plist"
tell application id "com.apple.SystemEvents" to tell the ¬
property list file fp to make new property list item ¬
with properties {name:"ScriptWindowState", value:¬
{selectedTab:"log"}}
@ChristoferK
ChristoferK / Chrome#(Re)Store URLs.applescript
Created November 15, 2019 21:20
[(Re)Store Chrome Tabs] Stores a list of open Chrome URLs, writing out to file and preserving arrangement; restores from file. #AppleScript #Chrome #Chromium #tabs #URLs #save #restore
property home : system attribute "HOME"
property file : home & "/.chrometabs"
on storeURLs()
close access (open for access my file)
tell application id "com.google.chrome" to ¬
set URLs to every window's tabs's URL
write the URLs to my file
@ChristoferK
ChristoferK / Discretionary URL Open vs Activate Existing Tab.applescript
Last active October 3, 2019 08:52
[If URL Open, Then: Activate Tab; Else: Open URL] A discerning URL opener for any Chromium-based web browser that eliminates the need for iteration. #AppleScript #Chromium #Open #URL #browser #Chrome #Brave
to open location www
--OR: application id "com.google.Chrome"
tell application id "com.brave.Browser" to tell windows to tell (it ¬
where integer 1 in ((tab 1's id where www is in URL) & 0) ¬
is in the id of every tab) & false to tell the first item
if it = false then return continue open location www
set i to character id (id of tab 1 where www is in URL)
set t to character id (id of tabs)
set the active tab index to the offset of i in t
activate
@ChristoferK
ChristoferK / Get The Front Document's Containing Folder.applescript
Last active September 29, 2019 16:35
[Front Document's Containing Folder] Retrieves, if available, the path to the frontmost application's active document, from which the parent folder is returned #AppleScript #document #path #file #folder #Accessibility #SystemEvents
tell application id "com.apple.systemevents" to tell (the first process ¬
where it is frontmost) to tell (a reference to the front window) ¬
to if it exists then tell its attribute "AXDocument"'s value to ¬
if it is not in [missing value, "file:///Irrelevent"] then ¬
return my (POSIXPathOfFolder for it)
false
on POSIXPathOfFolder for (fileURL as text)
local fileURL