• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: DefaultsLib [Was: Re: AS Library Question]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: DefaultsLib [Was: Re: AS Library Question]


  • Subject: Re: DefaultsLib [Was: Re: AS Library Question]
  • From: Shane Stanley <email@hidden>
  • Date: Tue, 22 Dec 2015 15:59:00 +1100

On 22 Dec 2015, at 3:55 PM, Shane Stanley <email@hidden> wrote:

So much explanation, the script will have to come in a follow-up message...

So here is the actual lib code. Save as DefaultsLib.scptd in ~/Library/Script Libraries.

use AppleScript version "2.3.1"
use scripting additions
use framework "Foundation"

script forInheritance
-- required for inheritance
end script

on makeDefaultsWithID:scriptId factoryValues:initialValues
script
property parent : forInheritance
property scriptDefaults : missing value


-- Storage handlers


-- This method is use for strings, records, lists, Cocoa dates
on setObject:anObject forKey:aKey
scriptDefaults's setObject:anObject forKey:aKey
end setObject:forKey:


on setInteger:anInteger forKey:aKey
scriptDefaults's setInteger:anInteger forKey:aKey
end setInteger:forKey:


on setReal:aDouble forKey:aKey
scriptDefaults's setDouble:aDouble forKey:aKey
end setReal:forKey:


on setBool:aBool forKey:aKey
scriptDefaults's setBool:aBool forKey:aKey
end setBool:forKey:


-- File storage methods


on setFile:aFile forKey:aKey
set thePath to POSIX path of theFile
set theURL to current application's class "NSURL"'s fileURLWithPath:thePath
scriptDefaults's setURL:theURL forKey:aKey
end setFile:forKey:


on setAlias:anAlias forKey:aKey
set thePath to POSIX path of anAlias
set theURL to current application's class "NSURL"'s fileURLWithPath:thePath
set theURL to theURL's fileReferenceURL()
scriptDefaults's setURL:theURL forKey:aKey
end setAlias:forKey:


-- Retrievers, returning AppleScript values


on stringForKey:aKey
return (scriptDefaults's stringForKey:aKey) as string
end stringForKey:


on listForKey:aKey
return (scriptDefaults's arrayForKey:aKey) as list
end listForKey:


on recordForKey:aKey
return (scriptDefaults's dictionaryForKey:aKey) as record
end recordForKey:


on integerForKey:aKey
return (scriptDefaults's integerForKey:aKey) as integer
end integerForKey:


on realForKey:aKey
return (scriptDefaults's doubleForKey:aKey) as real
end realForKey:


on boolForKey:aKey
return (scriptDefaults's boolForKey:aKey) as boolean
end boolForKey:


on fileForKey:aKey
set theURL to scriptDefaults's URLForKey:aKey
return (theURL's |path|() as text) as «class furl»
end fileForKey:


-- Handlers for dealing with AS dates


on registerDateDefault:aDate forKey:aKey
set theData to current application's NSKeyedArchiver's archivedDataWithRootObject:aDate
set aDictionary to current application's NSDictionary's dictionaryWithObject:theData forKey:aKey
scriptDefaults's registerDefaults:aDictionary
end registerDateDefault:forKey:


on setDate:aDate forKey:aKey
set theData to current application's NSKeyedArchiver's archivedDataWithRootObject:aDate
scriptDefaults's setObject:theData forKey:aKey
end setDate:forKey:


on dateForKey:aKey
set theData to scriptDefaults's objectForKey:aKey
return (current application's NSKeyedUnarchiver's unarchiveObjectWithData:theData) as date
end dateForKey:


-- Generic handlers for AppleScript values (dates, enumerators, etc)


on registerASDefault:aValue forKey:aKey
set theData to current application's NSKeyedArchiver's archivedDataWithRootObject:aValue
set aDictionary to current application's NSDictionary's dictionaryWithObject:theData forKey:aKey
scriptDefaults's registerDefaults:aDictionary
end registerASDefault:forKey:


on setASValue:aValue forKey:aKey
set theData to current application's NSKeyedArchiver's archivedDataWithRootObject:aValue
scriptDefaults's setObject:theData forKey:aKey
end setASValue:forKey:


on ASValueForKey:aKey
set theData to scriptDefaults's objectForKey:aKey
set aValue to (current application's NSKeyedUnarchiver's unarchiveObjectWithData:theData)
set anArray to current application's NSArray's arrayWithArray:{aValue}
return item 1 of (anArray as list)
end ASValueForKey:


end script
set ASPrefs to result
if scriptId = missing value then -- must be running as applet
set theDefaults to current application's NSUserDefaults's standardUserDefaults()
else
set theDefaults to current application's NSUserDefaults's alloc()'s initWithSuiteName:scriptId
end if
theDefaults's registerDefaults:initialValues
set ASPrefs's scriptDefaults to theDefaults
return ASPrefs
end makeDefaultsWithID:factoryValues:

-- 
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

References: 
 >Re: AS Library Question (From: has <email@hidden>)
 >Re: AS Library Question (From: Jean-Christophe Helary <email@hidden>)
 >Re: AS Library Question (From: Shane Stanley <email@hidden>)
 >Re: AS Library Question (From: Jean-Christophe Helary <email@hidden>)
 >Re: AS Library Question (From: Shane Stanley <email@hidden>)
 >Re: AS Library Question (From: "Stockly, Ed" <email@hidden>)
 >Re: AS Library Question (From: Jean-Christophe Helary <email@hidden>)
 >DefaultsLib [Was: Re: AS Library Question] (From: Shane Stanley <email@hidden>)

  • Prev by Date: DefaultsLib [Was: Re: AS Library Question]
  • Next by Date: Re: AS Library Question
  • Previous by thread: DefaultsLib [Was: Re: AS Library Question]
  • Next by thread: Re: AS Library Question
  • Index(es):
    • Date
    • Thread