Skip to content

Instantly share code, notes, and snippets.

@ChristoferK
ChristoferK / Menubar Applications.applescript
Last active June 7, 2019 07:49
[Menubar Applications] UI scripting commands in AppleScript that provide access to the menubar applications and their menubar icons #AppleScript #applications #menubar #SystemEvents #UI
use application "System Events"
set _P to a reference to (processes whose class of menu bar 2 = menu bar)
name of _P --> A list of apps sitting in the menu bar
menu bar item 1 of menu bar 2 of _P --> Their icons
@ChristoferK
ChristoferK / ISO 8601 Date & Time Representation.applescript
Last active June 7, 2019 07:45
[ISO Date] Returns the current date and time expressed according to ISO 8601 #AppleScript #date #time #ISO8601 #class_isot #now
# Note: «class isot» cannot be coerced to text
get the ((current date) as «class isot» as string)
--> "2018-03-28T14:52:26"
@ChristoferK
ChristoferK / System Events#Get Empty Sub-Folders.applescript
Last active June 5, 2019 12:49
[Get Empty Sub-Folders] Retrieves a list of empty sub-folders in a specified folder #AppleScript #System_Events #Finder #folders #empty #aliases
use application "System Events"
on directories of directory without contents
local contents
tell (a reference to the POSIX path of every folder in ¬
the folder named directory) to if contents then
@ChristoferK
ChristoferK / [JSObjC] Get Classes List.js
Last active May 19, 2019 02:10
[Objective-C Classes List] Retrieves an ordered, filtered list of Objective-C NSObject subclasses and writes them to file. #JXA #JavaScript #ObjectiveC #ObjC #classes #objc_copyClassList
ObjC.import('stdlib');
ObjC.import('Foundation');
__NSString__ = $.NSString.stringWithString.bind($.NSString);
ObjC.bindFunction('objc_copyClassList', ['void**', ['int']]);
ObjC.bindFunction('class_getName', ['char *', ['void*']]);
ObjC.bindFunction('class_getSuperclass', ['void*', ['void*']]);
ObjC.bindFunction('class_isMetaClass', ['bool *', ['void*']]);
i = 0; // Counter variable
@ChristoferK
ChristoferK / Open URLs in Safari.applescript
Created June 17, 2018 02:49
[Open URLs in Safari] An AppleScript that receives a list of URLs as input and opens them in new tabs of a specified existing window or, optionally, a new window, and can be set to do this in the background or by raising Safari to the foreground #Safari #tabs #URLs #AppleScript
property S : a reference to application "Safari"
global W
on run input -- input is a list of URLs
if (count input) is 0 then ¬
set the input to {¬
"alfredapp.com", ¬
"stackoverflow", ¬
"google.co.uk"}
@ChristoferK
ChristoferK / Redefining AppleScript Builtins.md
Last active April 25, 2019 15:18
[Redefining AppleScript Builtins] A series of command declarations that surplant the builtin definitions of some common AppleScript commands to increase versatility and functionality. #AppleScript #builtins #handlers #recursion #date #clipboard

A series of command declarations that surplant the builtin definitions of some common AppleScript commands to increase versatility and functionality:

  • do shell script Supply the shell command string optionally as a list of strings that will be concatenated and delimited using a space character

  • load script Use scripts stored in text files that can be referenced by posix paths utilising ~/, or any other type of AppleScript file reference (e.g. POSIX file, file, alias)

  • offset Return a list of all offsets of a substring. Includes recursive and iterative routines, the latter being the most efficient.

@ChristoferK
ChristoferK / Click Menu Item.applescript
Last active September 11, 2018 05:03
[Click Menu Item] A code refactoring of the original pair of handlers, now unified into a single handler that receives an application and a list of strings that represent a path through the application's menu tree to the menu item that is to be clicked. #AppleScript #recursion #menus #menu_items #System_Events
to clickMenuItem(pObj, mItems as list)
local pObj, mItems
if mItems = {} then return
script menuItem
use application "System Events"
property m0 : item 1 of mItems
property mN : rest of mItems
@ChristoferK
ChristoferK / Integer Factorisation.applescript
Last active August 21, 2018 08:32
[Prime Numbers] 1a. A superfast prime number generator that lists all primes up to and including N. 1b. A JavaScript prime number generating one-liner. 2. A fast integer factorisation handler that utilises the prime number generator. #AppleScript #JavaScript #primes #lists #math #integers #factorisation
to factorise(x)
script
property lim : x ^ 0.5
property P : primes(lim) & getNextPrime(lim)
property L : {}
end script
tell the result
repeat with p0 in its P
repeat until (x mod p0) ≠ 0
@ChristoferK
ChristoferK / Minimise Background Safari Windows.js
Last active August 7, 2018 22:46
[Minimise Background Safari Windows] Minimises all Safari windows except the one in focus. #JXA #JavaScript #Safari
windows = Application('Safari').windows()
windows.shift() // Remove front window from array
windows.map(w=>w.miniaturized = true)
@ChristoferK
ChristoferK / keybase.md
Last active June 24, 2018 22:21
Keybase Proof of Ownership

Keybase proof

I hereby claim:

  • I am ChristoferK on github.
  • I am christoferk (https://keybase.io/christoferk) on keybase.
  • I have a public key whose fingerprint is B621 0876 4E7A 713C F8F3 EC3A FD7A BF5E 1AB1 49E2

To claim this, I am signing this object: