How do I make my script write to a Property List file without having to create a ".plist" file on the user's computer, but rather directly in the current application?
Asked
Active
Viewed 572 times
0
-
Are you trying to create something like an "in memory" PLIST file which is never written to disk? Or are you after something else? And why would you want to use a PLIST file specifically if you are looking for a an "in memory" collection of information since then you are probably looking for an `NSDictionary`? – Fahim Mar 30 '17 at 01:18
-
@Fahim I am trying to save simple variables such as short strings and some booleans. – Mar 30 '17 at 07:24
-
Sorry, what I meant was, is there a specific reason for using the PLIST format? If you are not writing the file to disk, can't you use an `NSDictionary` instead? – Fahim Mar 30 '17 at 07:30
1 Answers
0
The plist files are created from the user defaults. So you need to save your values there.
Make a property to shorten writings at top
property NSUserDefaults : class "NSUserDefaults" of current application
to write
NSUserDefaults's standardUserDefaults()'s setObject_forKey_(yourValue, yourKey)
to read
NSUserDefaults's standardUserDefaults()'s object_forKey(yourKey)
Example
property NSUserDefaults : class "NSUserDefaults" of current application
set mytext to "Hello"
-- write
NSUserDefaults's standardUserDefaults()'s setObject_forKey_(mytext, "MyKey")
-- read it with
log NSUserDefaults's standardUserDefaults()'s object_forKey_("MyKey")
-- Hello
It's untested cause i don't have a mac here at the moment. But it should work.
Edit
To set standard values do the following in your application's appDelegate method applicationWillFinishLaunching:aNotification (don't forget to declare the property mentioned above):
on applicationWillFinishLaunching:aNotification
set prefs to {MyKey:"Hello", SomeOtherKey:true}
NSUserDefaults's standardUserDefaults()'s registerDefaults_(prefs)
end

Pat_Morita
- 3,355
- 3
- 25
- 36
-
How to I create a changeable default set to a boolean, and write to that? I don't have any experience with "log", but how do I set a global variable to whatever I previously set as a "user default"? – Mar 30 '17 at 08:17
-
Updated the answer. And with log you can output values to your console. Log works for ASOC projects while for pure OBJC projects you only can use NSLog. If you don't know log you should spend some time with debugging – Pat_Morita Mar 30 '17 at 08:41
-
can I use this code to set my variable within the script to a user default? like this: `set theVariable to NSUserDefaults's standardUserDefaults()'s object_forKey_("MyKey")` – Mar 30 '17 at 17:03
-
Can I do this in my script: `set theVariable to NSUserDefaults's standardUserDefaults()'s object_forKey_("MyKey")` – Mar 31 '17 at 05:09
-
-
-
-.- if you would just try... you'd see it fails. This method has one parameter. One parameter, one underscore. object_forKey ("MyKey"). I explained that to you in another question already. – Pat_Morita Mar 31 '17 at 05:21