Skip to content

Instantly share code, notes, and snippets.

@sardisson
Last active February 21, 2019 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sardisson/4c16e00377e2bc36adc6dd9c219e001a to your computer and use it in GitHub Desktop.
Save sardisson/4c16e00377e2bc36adc6dd9c219e001a to your computer and use it in GitHub Desktop.
AppleScript routines for reading and writing NSUserDefaults ("preferences") and for prompting for an API token and storing it in a NSUserDefault.
(*
* AppleScript routines for reading and writing NSUserDefaults ("preferences")
* and for prompting for an API token and storing it in a NSUserDefault.
*)
-- NSUserDefaults domain and key names
property theDomain : "info.johnjohnston.micro-blog-fav-images"
property mbKeyName : "micro-api-token"
property cloudKeyName : "cloud-api-token"
-- strings for Micro.blog & cloudimage.io API token prompts
property mbTitle : "Please enter an API Token for Micro.blog"
property mbMessage : "Available from https://micro.blog/account/apps > App name: Generate Token"
property cloudTitle : "Please enter an API Token for cloudimage.io"
property cloudMessage : "Available from the website"
-- fetch API tokens from defaults, or prompt for token if no stored value
set mbAPIToken to my defaultsRead(theDomain, mbKeyName)
if mbAPIToken is missing value then set mbAPIToken to my promptForAPITokenAndStore(mbTitle, mbMessage, mbKeyName)
-- API token can still be null at this point due to errors, etc.
set cloudAPIToken to my defaultsRead(theDomain, cloudKeyName)
if cloudAPIToken is missing value then set cloudAPIToken to my promptForAPITokenAndStore(cloudTitle, cloudMessage, cloudKeyName)
-- API token can still be null at this point due to errors, etc.
-- routines for reading and writing defaults and prompting for API tokens
-- defaultsRead() only supports string, integer (with cast), and boolean (with double cast) key types
-- for integers, cast the defaultsRead() call "as number"; for booleans, "as number as boolean"
-- this can return a null value in cases of failure
on defaultsRead(theDomain, keyName)
try
set theValue to (do shell script "defaults read " & theDomain & " " & keyName)
on error errTxt number errNum
set theValue to missing value
end try
return theValue
end defaultsRead
-- this can fail (most likely if you mix up variable order) and currently won't tell you
on defaultsWrite(theDomain, keyName, value, type)
try
do shell script "defaults write " & theDomain & space & keyName & space & "-" & type & space & value
on error errTxt number errNum
end try
end defaultsWrite
-- this can return null values
on promptForAPITokenAndStore(theTitle, theMessage, keyName)
try
activate
set theValue to text returned of (display dialog theMessage with title theTitle default answer "" with icon note buttons {"OK"} default button 1)
on error errTxt number errNum
set theValue to missing value
end try
if theValue is not in {"", missing value} then my defaultsWrite(theDomain, keyName, theValue, "string")
return theValue
end promptForAPITokenAndStore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment