Apple Script Question

Discussion in 'Mac Programming' started by Let's Sekuhara!, May 18, 2012.

  1. macrumors 6502

    Let's Sekuhara!

    Joined:
    Jun 30, 2008
    Location:
    日本
    #1
    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!
     
  2. kryten2, May 18, 2012
    Last edited: May 18, 2012

    macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #2
    I took a look at the provided link. Nothing too fancy going on there, just setting some properties.

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

    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 :

    Code:
    defaults write ~/Library/Preferences/com.apple.TextEdit RichText -bool false
    You can use this in Script Editor by using do shell script like :

    Code:
    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
     
  3. thread starter macrumors 6502

    Let's Sekuhara!

    Joined:
    Jun 30, 2008
    Location:
    日本
    #3
    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?
     
  4. thread starter macrumors 6502

    Let's Sekuhara!

    Joined:
    Jun 30, 2008
    Location:
    日本
    #4
    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)?
    Code:
    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.
     
  5. kryten2, May 31, 2012
    Last edited: May 31, 2012

    macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #5
    To answer your questions :

    1. 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.
    2. You've made an error in your command. Key and value are wrong.
      Quote from the link I gave you :


    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 :

    Code:
    	<key>PlainTextEncodingForWrite</key>
    	<[COLOR="Red"]integer[/COLOR]>4</[COLOR="red"]integer[/COLOR]>
    
    So the right command would be :

    Code:
    defaults write ~/Library/Preferences/com.apple.TextEdit PlainTextEncodingForWrite -int 4
    Open Script Editor --> go to Window-->Library
     

    Attached Files:

  6. thread starter macrumors 6502

    Let's Sekuhara!

    Joined:
    Jun 30, 2008
    Location:
    日本
    #6
    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?
     
  7. kryten2, Jun 1, 2012
    Last edited: Jun 1, 2012

    macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #7
    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.
     

    Attached Files:

Share This Page