Skip to content

Instantly share code, notes, and snippets.

@loshlee
Last active April 18, 2023 07:58
Show Gist options
  • Save loshlee/5740427 to your computer and use it in GitHub Desktop.
Save loshlee/5740427 to your computer and use it in GitHub Desktop.
I use this AppleScript to launch Slate with the last used configuration, edit a slate configuration from a collection of .slate or .slate.js files using BBEdit (demo mode), or activate one of the collected configuration files in macOS. Slate is a souped up Window Manager (https://github.com/jigish/slate)
set default_settings to "# This is a sample .slate file.
# If no ~/.slate file exists, this is the file that will be used.
# Action summary
# ⌃⌥⌘0 - Show window locations for current screen
# ⌃⌥⌘1 - Move and resize front window to upper left corner
# ⌃⌥⌘2 - Move and resize front window to upper right corner
# ⌃⌥⌘3 - Move and resize front window to lower left corner
# ⌃⌥⌘4 - Move and resize front window to lower right corner
# ⌃⌘1 - Move and resize front window to left Half
# ⌃⌘2 - Move and resize front window to right Half
# ⇧⌥1 - Grow front window +10% to right
# ⇧⌥2 - Shrink front window -10% from right
# ⇧⌥3 - Grow front window +10% toward bottom
# ⇧⌥4 - Shrink front window -10% from bottom
# ⌃⌥↑ - Move front window 10% toward top of screen
# ⌃⌥→ - Move front window 10% toward right of screen
# ⌃⌥← - Move front window 10% toward left of screen
# ⌃⌥↓ - Move front window 10% toward bottom of screen
# ⌃⇧4 - Move and resize 2 frontmost windows to left and right halves of screen (press twice)
# ⌃⎋ (control,escape) - Use a grid to draw a size and location for the front window
# Configs
config defaultToCurrentScreen true
config windowHintsShowIcons true
config switchBackgroundColor 50;53;58;0.6
config windowHintsSpread true
config windowHintsFontColor 255;46;86;0.9
config windowHintsIconAlpha 1
config windowHintsRoundedCornerSize 15
config windowHintsFontName Geneva
config windowHintsFontSize 96
config nudgePercentOf screenSize
config resizePercentOf screenSize
config secondsBetweenRepeat 0.1
config checkDefaultsOnLoad true
config focusCheckWidthMax 3000
config keyboardLayout qwerty
config windowHintsIgnoreHiddenWindows false
config windowHintsDuration 8
#config windowHintsOrder persist
# Keys ctrl;shift;alt;cmd
# aliases
alias showNormalHint hint ASDFGHJKLQWERTYUIOPCVBN
alias full move screenOriginX;screenOriginY screenSizeX;screenSizeY
alias lefthalf move screenOriginX;screenOriginY screenSizeX/2;screenSizeY
alias righthalf move screenOriginX+screenSizeX/2;screenOriginY screenSizeX/2;screenSizeY
alias topleft corner top-left resize:screenSizeX/2;screenSizeY/2
# alias topright corner top-right resize:screenSizeX/2;screenSizeY/2
alias topright move screenOriginX+screenSizeX/2;screenOriginY screenSizeX/2;screenSizeY/2
alias bottomleft corner bottom-left resize:screenSizeX/2;screenSizeY/2
alias bottomright corner bottom-right resize:screenSizeX/2;screenSizeY/2
bind esc:ctrl grid #padding:3 0:12,8 # padding:3 0:12,4 # for second screen 1:8,3
# location Bindings
bind 0:ctrl;alt;cmd ${showNormalHint}
bind 1:ctrl;alt;cmd sequence ${topleft} # > push up > push left
bind 2:ctrl;alt;cmd sequence ${topright} > push up > push right
bind 3:ctrl;alt;cmd sequence ${bottomleft} > push down > push left
bind 4:ctrl;alt;cmd sequence ${bottomright} > push down > push right
# bind 4:ctrl;alt;cmd ${bottomright}
bind 1:ctrl;cmd ${lefthalf}
bind 2:ctrl;cmd ${righthalf}
bind 1:alt;shift resize +10% +0 bottom-left
bind 2:alt;shift resize -10% +0 bottom-left
bind 3:alt;shift resize +0 +10% top-right
bind 4:alt;shift resize +0 -10% top-right
# Move
bind up:ctrl;alt nudge +0 -100
bind down:ctrl;alt nudge +0 +100
bind left:ctrl;alt nudge -100 +0
bind right:ctrl;alt nudge +100 +0
bind 4:ctrl;shift sequence push left bar-resize:screenSizeX/2 > focus right > push right bar-resize:screenSizeX/2 > push left > push right | push left
"
set myHome to ((path to home folder) as string)
set _path to myHome & "Slate sets:"
set targ1 to POSIX path of _path
set targ2 to POSIX path of myHome
set user_dotSlate to (targ2 & ".slate")
set user_dotSlate_js to (targ2 & ".slate.js")
set user_slates to {user_dotSlate, user_dotSlate_js}
set the item_count to the number of items in user_slates
repeat with i from 1 to the item_count
set this_item to item i of user_slates
try
if (POSIX file (this_item)) as alias exists then
if i is 1 then -- first list item
set has_dotSlate to true
else if i is the item_count then -- last list item
set has_dotSlate_js to true
end if
end if
on error
if i is 1 then -- first list item
set has_dotSlate to false
else if i is the item_count then -- last list item
set has_dotSlate_js to false
end if
end try
end repeat
try
do shell script "mkdir" & space & quoted form of (POSIX path of _path)
end try
try
--display dialog (quoted form of default_settings) & space & ">" & space & quoted form of (POSIX path of _path)
set name_target to alias (POSIX file (targ1 & "default.slate")) --file name is already taken at destination if no error occurs.
on error
do shell script "echo" & space & (quoted form of default_settings) & space & ">" & space & quoted form of (POSIX path of _path) & "default.slate"
end try
if has_dotSlate then
try
set name_target to alias (POSIX file (targ1 & "original.slate")) --file name is already taken at destination if no error occurs.
on error
my backupdotslate(user_dotSlate, (targ1 & "original.slate"))
end try
end if
if has_dotSlate_js then
try
set name_target to alias (POSIX file (targ1 & "original.slate.js")) --file name is already taken at destination if no error occurs.
on error
my backupdotslate(user_dotSlate_js, (targ1 & "original.slate.js"))
end try
end if
set ls to (list folder _path)
set ld to {}
repeat with i in ls
if i is not in {".DS_Store"} then
set ld to ld & i
end if
end repeat
display dialog "Edit current configuration or continue?" buttons {"Open Current Configuration", "Continue"}
if button returned of the result is "Open Current Configuration" then
try
--display dialog "open -a BBEdit.app" & space & (quoted form of targ2) & (".slate")
do shell script "open -a BBEdit.app" & space & (quoted form of targ2) & (".slate")
my bounce_Slate()
on error
try
do shell script "open -a BBEdit.app" & space & (quoted form of targ2) & (".slate.js")
my bounce_Slate()
on error number error_number
if the error_number is -128 then
error number -128
end if
end try
end try
else
try
display dialog "Open configuration file for editing or continue launching Slate?" buttons {"Last used", "Edit Configuration", "Activate Configuration"} default button 2
if button returned of the result is "Edit Configuration" then
try
choose from list ld with prompt "Choose Slate configuration to open in BBEdit:"
do shell script "open -a BBEdit.app" & space & (quoted form of targ1) & (quoted form of (result as text))
my bounce_Slate()
on error number error_number
if the error_number is -128 then
error number -128
end if
end try
else if button returned of the result is "Last used" then
my bounce_Slate()
else
choose from list ld with prompt "Choose Slate configuration to activate:"
if result is false then
error number -128
else
if (result as text) ends with ".js" then
do shell script "rm" & space & user_dotSlate & ";" & "ditto" & space & (quoted form of targ1) & (quoted form of (result as text)) & space & user_dotSlate_js
else
do shell script "rm" & space & user_dotSlate & ";" & "ditto" & space & (quoted form of targ1) & (quoted form of (result as text)) & space & user_dotSlate
try
do shell script "rm" & space & user_dotSlate_js
end try
end if
my bounce_Slate()
end if
end if
on error
do shell script ("echo \"" & default_settings & space & "\" > " & space & (user_dotSlate))
end try
end if
on backupdotslate(original_Slate, backup_Target)
set thesource to (quoted form of (POSIX path of original_Slate))
do shell script "ditto" & space & thesource & space & quoted form of backup_Target
end backupdotslate
on bounce_Slate()
tell application "System Events"
set appname to "Slate"
if appname is in name of every process then
set the_process to the process appname
set pid to the unix id of the_process
else
set pid to ""
end if
end tell
if pid is not "" then do shell script ("kill -9 " & pid)
tell application "Finder" to open application file id "com.slate.Slate"
end bounce_Slate
@loshlee
Copy link
Author

loshlee commented Mar 4, 2021

When first run, the script creates a Slate Sets folder in the Home folder containing a copy of the default slate settings file which can be edited and/or activated on subsequent launches. If any ".slate" or ".slate.js" file exists in the Home folder when Slate Manager is first launched, backup copies for those files are saved into the Slate Sets folder in the Home folder. The backup copy of the ".slate" file, is saved as a file named "original.slate" in the Slate Sets folder in the Home folder. The backup copy of the ".slate.js" file, if it exists, is saved as a file named "original.slate.js". If a ".slate" file is selected for activation, it removes the ".slate.js" file from the Home folder. Put a ".slate.js" back by selecting one using the script.

In March of 2021 I updated this so that I could also view the existing .slate or .slate.js file in the Home folder. I included an Action Summary at the top to make it clear exactly how the supplied example lets you use Slate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment