IS: Script Editor Styles Format Change Script -- WAS: Re: String to list conversion
IS: Script Editor Styles Format Change Script -- WAS: Re: String to list conversion
- Subject: IS: Script Editor Styles Format Change Script -- WAS: Re: String to list conversion
- From: Johnny AppleScript <email@hidden>
- Date: Tue, 14 Sep 2004 04:56:39 -0600
Title: IS: Script Editor Styles Format Change Script -- WAS: Re: String to list conversion
Here’s YA version of a script that will swap out up to two SE format styles, plus default settings, also allowing you to record two styles to your existing SE prefs plist. It could stand some more error handling, user feedback, and probably a list of additional styles, but three should be enough for anyone.
(* script to change Script Editor formatting prefs; tested in SE 2.0 (v43) & OS X 10.3.3 *)
global newText, operators, languageKeys, applicationKeys, comments, values, variables, referenceKeys, applescriptPrefs, sp, pListPath, keyPref, defPrefs
set pListPath to "~/Library/Preferences/com.apple.applescript"
set keyPref to " AppleScriptTextStyles"
set keyPref2 to " AppleScriptTextStyles1"
set keyPref3 to " AppleScriptTextStyles2"
set applescriptPrefs to ""
set defPrefs to {"'Courier;p;12;32768 0 32768'", "'Verdana;p;12;0 0 0'", "'Verdana;b;12;0 0 65535'", "'Verdana;p;12;0 0 65535'", "'Verdana;i;12;19660 19960 19960'", "'Verdana;p;12;0 0 0'", "'Verdana;p;12;16384 32768 0'", "'Verdana;p;12;32768 0 32768'"}
set sp to " " -- need spaces in terminal ops
set {a, b, c} to {"Cancel", "Record Current Styles", "Change Styles"}
set runOption to button returned of (display dialog "Script Editor Format Style Tool." & return & return & "What would you like to do?" buttons {a, b, c})
if runOption is b then
set applescriptPrefs to do shell script "defaults read " & pListPath & keyPref -- get current styles
set {newText, operators, languageKeys, applicationKeys, comments, values, variables, referenceKeys} to my parsePrefs(applescriptPrefs)
set {a, b, c} to {"Cancel", "Style 1", "Style 2"}
set styleOption to button returned of (display dialog "Select the style assignment name for the current style settings:" buttons {a, b, c})
if styleOption is b then set keyPref to keyPref2
if styleOption is c then set keyPref to keyPref3
display dialog "Are you sure you want to overwrite any existing styles that may be assigned to '" & styleOption & "' ?" & return & return & "This cannot be undone."
my writePrefs()
else if runOption is c then
set {a, b, c} to {"default style", "Style 1", "Style 2"}
set formatStyle to button returned of (display dialog "Reset Script Editor formatting preferences?" buttons {a, b, c})
display dialog "Are you sure you want to change the current formatting prefs to '" & formatStyle & "' ?"
if formatStyle is a then set applescriptPrefs to "()"
if formatStyle is b then set applescriptPrefs to my readPrefs(keyPref2)
if formatStyle is c then set applescriptPrefs to my readPrefs(keyPref3)
set {newText, operators, languageKeys, applicationKeys, comments, values, variables, referenceKeys} to my parsePrefs(applescriptPrefs)
my writePrefs()
end if
set {a, b, c} to {"Later", "Just Quit", "Quit and Relaunch"}
set quitOption to button returned of (display dialog "You must quit Script Editor and relaunch for style changes to take effect." buttons {a, b, c})
if quitOption is b then tell application "Script Editor" to quit
if quitOption is c then
(* add a routine to get a list of open windows and reopen them after launch *)
tell application "Script Editor" to quit
(* this line will fail depending on weather SE is running the script, or another app invokes it; works great from Keyboard Maestro *)
tell application "Script Editor" to activate
end if
on readPrefs(keyPref)
try
do shell script "defaults read " & pListPath & keyPref
on error errMSG
log errMSG
set {newText, operators, languageKeys, applicationKeys, comments, values, variables, referenceKeys} to defPrefs
my writePrefs()
my readPrefs(keyPref)
end try
end readPrefs
on writePrefs()
set defWrite to "defaults write " & pListPath & keyPref & " -array"
if applescriptPrefs is not "" then
set prefsChange to defWrite & sp & newText & sp & operators & sp & languageKeys & sp & applicationKeys & sp & comments & sp & values & sp & variables & sp & referenceKeys
else
set prefsChange to defWrite
end if
try
do shell script prefsChange -- execute the changes
on error errMSG
display dialog errMSG
end try
end writePrefs
on parsePrefs(applescriptPrefs)
set applescriptPrefs to every paragraph of applescriptPrefs
log {applescriptPrefs}
if applescriptPrefs contains "()" or applescriptPrefs is "" then
set parsedPrefs to defPrefs
else
set parsedPrefs to {}
repeat with i from 2 to ((number of items in applescriptPrefs) - 1)
set a to item i of applescriptPrefs
if a ends with ", " then set e to -4 -- "\","
if a ends with "\"" then set e to -2 -- "\""
set a to ("'" & characters ((offset of "\"" in a) + 1) thru e in a as string) & "'"
set end of parsedPrefs to a
end repeat
end if
return parsedPrefs
end parsePrefs
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden