applescript

How to get the localized name of a folder also called displayed name with AppleScript

Tagged i18n, displayed name, applescript, localized  Languages applescript

Not using the localized name of folders or items is a basic mistake made in almost all AppleScript examples I've seen.

For example, this code won't work on a Swedish OSX where the Applications folder's name is Program:

tell application "Finder"
    -- Mount a DMG
    .
    .
    .
    set position of item "Applications" to {175, 65}
end tell

The correct way of referencing folders is by using the localized name which you can get through the displayed name attribute like this:

tell application "Finder"
    -- Mount a DMG
    .
    .
    .
    set applications_folder to displayed name of (path to applications folder) -- i18n name
    set position of item applications_folder to to {175, 65}
end tell

This is the line which retrieves the localized name of the folder and stores it in a variable named applications_folder:

set applications_folder to displayed name of (path to applications folder) -- i18n name

How to Open Safari's Web Inspector With Applescript to Debug a Phonegap App

Tagged safari, web-inspector  Languages applescript

This trick will save years of your life. This applescript will make Safari's Web Inspector connect to a Phonegap application running on my iPhone that was started through XCode:

#!/usr/bin/osascript

set device_name to "Christian Hellsten iPhone"
tell application "Safari"
    activate
    tell application "System Events"
        try
            click menu item "index.html" of menu device_name of menu item device_name of menu "Utvecklare" of menu bar item "Utvecklare" of menu bar 1 of application process "Safari"
        end try
    end tell
end tell

Save the file, as e.g. open-web-inspector.applescript, then execute it:

chmod +x open-web-inspector.applescript
./open-web-inspector.applescript

For English version, change "Utvecklare" to "Develop".

Troubleshooting

  1. Error -1719 most likely means you need to enable assistive access for iTerm or whatever app you're running the script from.
  1. Error -10810 means "bad luck" in Applescript. Try restarting the computer.

If all else fails, run the script using the Applescript Editor. Also see, this script.

Tested on OSX Yosemite.