PDA

View Full Version : Apple Script Question




Let's Sekuhara!
May 18, 2012, 06:08 PM
Hi,

Pardon my newness to Apple Script, but I'd like to learn how to write scripts that can alter the Preferences of an OS X Application.

I found some info on it here:
https://gist.github.com/232524

What I'm not clear on is how to find out what the names of those prefs will be in the script.
For instance, in the example shown on the page from the above link - there is an object(?) in the script called "minimize effect".
But how do I find out the name of the object associated with a preference?

In Text Edit if I wanted to find out the name of the object that controls the default new document format (Rich or Plain text) how would I get that?

Thanks!



kryten2
May 18, 2012, 09:14 PM
I took a look at the provided link. Nothing too fancy going on there, just setting some properties.

For instance, in the example shown on the page from the above link - there is an object(?) in the script called "minimize effect".

That's a property of the dock preferences object. You can find that in the Dock Preferences Suite of the System Events dictionary.

In Text Edit if I wanted to find out the name of the object that controls the default new document format (Rich or Plain text) how would I get that?

Lots of applications store their preferences in a plist file. In case of TextEdit that would be ~/Library/Preferences/com.apple.TextEdit.plist. You can read or change this file with the defaults command. For example this command would change the default new document format from Rich text to Plain text :

defaults write ~/Library/Preferences/com.apple.TextEdit RichText -bool false

You can use this in Script Editor by using do shell script like :

do shell script "defaults write ~/Library/Preferences/com.apple.TextEdit RichText -bool false"


You can look for key values by making the settings manually in the Preferences window of TextEdit, quit TextEdit.app and look in the preference file what changed.

More info :

http://www.macosxautomation.com/applescript/features/system-prefs.html
http://www.macosxautomation.com/applescript/features/propertylists.html
http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/defaults.1.html

Let's Sekuhara!
May 31, 2012, 04:11 AM
Thank you for your reply!

While I read it immediately, I have not had time to really explore all of the information until now.

Can you please tell me more about this System Events dictionary that you mentioned? Is it an application in OS X? Or is it a list that exists within the AppleScript Editor app? Or is it just a document I can find online?

Let's Sekuhara!
May 31, 2012, 06:01 AM
OK, some more things I've discovered...

(1) I happen to have the Property List Editor installed because I installed Developer Tools. This makes it easy to see the names of the various properties as long as I am on my own Mac.
However, I'm still curious how it could be done on a Mac that doesn't have Property List Editor installed. Please enlighten.

(2) Why does the following command render TextEdit unable to launch (until the .plist file is deleted)?
defaults write ~/Library/Preferences/com.apple.TextEdit PlainTextEncoding 4
According to TextEdit's .plist file this is a perfectly valid property with a perfectly valid value.
If you don't believe me, try going to TextEdit's Preferences under the Open and Save tab and set "Saving files: Unicode (UTF-8)"
Quit TextEdit, then view the changes made to the .plist file.

Problem is, I can't seem to set it via the shell command - it just screws up the .plist file somehow.

kryten2
May 31, 2012, 09:23 AM
To answer your questions :


You can open/change plist files with TextEdit too. You might run into problems doing that. See this thread for some answers :http://forums.macrumors.com/showthread.php?t=944117
You can look at it with Quick Look.
You've made an error in your command. Key and value are wrong.
Quote from the link I gave you :

Specifying value types for preference keys:

If no type flag is provided, defaults will assume the value is a string. For best results,
use one of the type flags, listed below.

-string Allows the user to specify a string as the value for the given preference key.

-data Allows the user to specify a bunch of raw data bytes as the value for the given preference
key. The data must be provided in hexidecimal.

-int[eger] Allows the user to specify an integer as the value for the given preference key.

-float Allows the user to specify a floating point number as the value for the given preference
key.

-bool[ean] Allows the user to specify a boolean as the value for the given preference key. Value must
be TRUE, FALSE, YES, or NO.

-date Allows the user to specify a date as the value for the given preference key.

-array Allows the user to specify an array as the value for the given preference key:

defaults write somedomain preferenceKey -array element1 element2 element3

The specified array overwrites the value of the key if the key was present at the time of
the write. If the key was not present, it is created with the new value.

-array-add Allows the user to add new elements to the end of an array for a key which has an array as
its value. Usage is the same as -array above. If the key was not present, it is created
with the specified array as its value.

-dict Allows the user to add a dictionary to the defaults database for a domain. Keys and values
are specified in order:

defaults write somedomain preferenceKey -dict key1 value1 key2 value2

The specified dictionary overwrites the value of the key if the key was present at the time
of the write. If the key was not present, it is created with the new value.

-dict-add Allows the user to add new key/value pairs to a dictionary for a key which has a dictionary
as its value. Usage is the same as -dict above. If the key was not present, it is created
with the specified dictionary as its value.


I've made the change in TextEdit's Preferences under the Open and Save tab and set "Saving files: Unicode (UTF-8)". If it's the setting you see in the attached tumbnail the following key/value was added in my plist file on my system. I'm on Leopard :

<key>PlainTextEncodingForWrite</key>
<integer>4</integer>


So the right command would be :

defaults write ~/Library/Preferences/com.apple.TextEdit PlainTextEncodingForWrite -int 4

Can you please tell me more about this System Events dictionary that you mentioned?

Open Script Editor --> go to Window-->Library

Let's Sekuhara!
Jun 1, 2012, 02:47 PM
Thank you very much for the detailed explanation! Everything you explained makes perfect sense.

I'm still trying to understand where all the settings are stored in the system.
Currently looking for language input settings and the keyboard shortcuts to toggle them.

----------

Also, is it possible to package graphics inside of an app made with AppleScript Editor?

For example, say I want to make a script that mods the Dock image.
Can I put the graphics that are meant to replace the original graphics inside of the app?

kryten2
Jun 1, 2012, 09:28 PM
Also, is it possible to package graphics inside of an app made with AppleScript Editor?

I believe you can although I can be wrong. Look at this thread for some info:
http://forums.macrumors.com/showthread.php?t=1369788
It's about adding a printer driver but I think the same principle can be applied for any kind of file.

Take a look at the attached zipfile. It's an app someone made to replace dock images. You can open it in Script Editor to see the script and change it for your purpose.