Skip to content

Instantly share code, notes, and snippets.

@ChristoferK
ChristoferK / YouTube Video Playing Status.applescript
Last active June 17, 2018 02:56
[YouTube Video State] Returns `playing`, `paused`, or `ended` to signify the state of a YouTube video open in Safari browser #AppleScript #YouTube #video #playing #Safari #JavaScript
tell application "Safari" to tell front window to tell ¬
(first tab whose URL contains "youtube.com/watch?v=") to ¬
if it exists then do JavaScript [¬
"document", ¬
".querySelector('div[class*=\"-mode\"]')", ¬
".className", ¬
".match(/(playing|paused|ended)-mode/)[1]"] as text
@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 / Dock Applications.applescript
Last active June 17, 2018 02:54
[Dock Applications] Retrieves a list of applications in the dock that are currently running #AppleScript #System_Events #UI #dock #applications
tell application "System Events" to tell ¬
process "Dock" to tell ¬
list 1 to get the name of ¬
(every UI element whose ¬
value of attribute "AXSubrole" is "AXApplicationDockItem" and ¬
value of attribute "AXIsApplicationRunning" is true)
@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 / 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 / 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:

@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 / 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 / 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)