Skip to content

Instantly share code, notes, and snippets.

@RobTrew
Created November 18, 2012 15:39
Show Gist options
  • Save RobTrew/4105865 to your computer and use it in GitHub Desktop.
Save RobTrew/4105865 to your computer and use it in GitHub Desktop.
Select FoldingText theme, and restart FoldingText
-- Permission is hereby granted, free of charge,
-- to any person obtaining a copy of this software
-- and associated documentation files (the "Software"),
-- to deal in the Software without restriction,
-- including without limitation the rights to use, copy,
-- modify, merge, publish, distribute, sublicense,
-- and/or sell copies of the Software, and to permit persons
-- to whom the Software is furnished to do so,
-- subject to the following conditions:
-- *******
-- The above copyright notice and this permission notice
-- shall be included in ALL copies
-- or substantial portions of the Software.
-- *******
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
-- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
-- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
-- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
-- OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
property pTitle : "Select theme and restart FT"
property pVer : "0.01"
property pAuthor : "Rob Trew"
-- ASSUMES THAT YOU HAVE PLACED TWO OR MORE THEMES, WITH NAMES OTHER THAN USER.FTTHEME,
-- IN THE THEME FOLDER (set pstrThemeFolder below)
property pstrThemeFolder : "$HOME/Library/Containers/com.foldingtext.FoldingText/Data/Library/Application Support/FoldingText/Themes/"
property pstrThemeExtn : ".fttheme"
property pActiveTheme : "User"
property pblnBackup : true -- edit to false if you don't want a stream of time-stamped backups of displaced themes
on run
-- GET A LIST OF THE THEMES IN THE THEME FOLDER WHICH ARE *NOT* CALLED USER.FTTHEME
set lstThemes to {}
repeat with oTheme in (paragraphs of (do shell script "ls \"" & pstrThemeFolder & "\" | sort"))
if (not (oTheme begins with "User.")) and (oTheme ends with pstrThemeExtn) ¬
then set end of lstThemes to text 1 thru -9 of (contents of oTheme)
end repeat
-- IF NO USER THEMES ARE FOUND, REMIND USER WHERE TO PUT THEM
if length of lstThemes < 1 then
tell application "Finder"
display dialog "No user themes found ..." & return & return & ¬
"Copy one or more " & pstrThemeExtn & " themes to:" & return & return & ¬
pstrThemeFolder buttons {"OK"} default button "OK" with title pTitle & " ver. " & pVer
do shell script "open \"" & pstrThemeFolder & "\""
activate
end tell
else -- IF A THEME HAS BEEN CHOSEN, CLOSE FOLDINGTEXT, BACK UP THE ACTIVE THEME, AND ACTIVATE THE CHOICE
tell application id "sevs"
activate
set varChoice to choose from list lstThemes with title pTitle & tab & pVer with prompt ¬
"Choose theme:" default items {item 1 of lstThemes} ¬
OK button name "OK" cancel button name "Cancel" with empty selection allowed without multiple selections allowed
end tell
if varChoice = false then return missing value
SwitchTo(item 1 of varChoice)
end if
-- RELAUNCH FOLDINGTEXT
do shell script "open -a FoldingText.app"
tell application "FoldingText" to activate
end run
on SwitchTo(strTheme)
try
tell application "FoldingText" to quit
end try
-- BACK UP ANY EXISTING THEME TO A TIME-STAMPED COPY
set strActiveTheme to quoted form of GetThemePath(pActiveTheme)
if pblnBackup then
if IsFolder(strActiveTheme) then
set strThemeFolder to do shell script "echo \"" & pstrThemeFolder & "\""
do shell script "mv " & strActiveTheme & space & ¬
quoted form of (strThemeFolder & "User." & TimeString() & pstrThemeExtn)
end if
end if
-- MAKE AN ACTIVE COPY OF THE CHOSEN THEME
do shell script "cp -R " & quoted form of GetThemePath(strTheme) & space & strActiveTheme
end SwitchTo
on GetThemePath(strTheme)
do shell script "echo \"" & pstrThemeFolder & strTheme & pstrThemeExtn & "\""
end GetThemePath
on IsFolder(strPath)
(do shell script ("test -d " & strPath & "; echo $?")) = "0"
end IsFolder
on TimeString()
set dte to (current date)
set {dlm, my text item delimiters} to {my text item delimiters, "-"}
tell dte
set lstDate to {its year, (its month) as integer, its day}
set strDate to (lstDate as text) & "-" & its hours & "h" & its minutes & "m" & its seconds & "s"
end tell
set my text item delimiters to dlm
return strDate
end TimeString
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment