KISS 🇺🇦

Stop the war!

Stop the war in Ukraine! Fuck putin!

More information is at: https://war.ukraine.ua/.

There is a fund to support the Ukrainian Army: https://savelife.in.ua/en/donate/, and there is a special bank account that accepts funds in multiple currencies: https://bank.gov.ua/en/about/support-the-armed-forces. I donated to them. Please donate if you can!

Killer putin

Killer putin. Source: politico.eu.

Arrested putin

"It hasn't happened yet, but it will happen sooner or later. Beautiful photo, isn't it?" Source: twitter.

Xcode and AppleScript

| comments

A few pieces of AppleScript automating project’s destination selection in Xcode. AFAIK, unfortunately there is no other way of controlling Xcode from outside, so we have to depend on the actual menu items of the interface.

Getting a list of available destinations

1
2
3
4
5
6
7
8
9
on get_destinations()
    tell application "System Events"
        tell process "Xcode"
            return title of menu items of menu 1 of menu item "Destination" of menu 1 of menu bar item "Product" of menu bar 1
        end tell
    end tell
end get_destinations

get_destinations()

If you run this the first time, you’re likely encounter this message: "AppleScript Editor.app" would like to control this computer using accessibility features. So you’ll need to grant access in System Preferences.

So the script above will output something like:

1
{"Select Next Destination", "Select Previous Destination", "", "iOS Device", "", "iOS Simulator", "iPhone Retina (3.5-inch)", "iPhone Retina (4-inch)", "iPhone Retina (4-inch 64-bit)", "iPad", "iPad Retina", "iPad Retina (64-bit)", "", "More Simulators…"}

Setting a destination

1
2
3
4
5
6
7
8
9
10
11
on set_destination(destination)
    tell application "System Events"
        tell process "Xcode"
            click menu item destination of menu 1 of menu item "Destination" of menu 1 of menu bar item "Product" of menu bar 1

            click ((first pop up button of list 1 of group 2 of toolbar 1 of front window) whose description is "Active Run Destination")
        end tell
    end tell
end set_destination

set_destination("iOS Device")

Here you pass an actual destination string from the above as the parameter, eg. "iOS Device". The first line in the innermost code block actually selects the destination, while the second one (optional) allows you to open the destinations popup so that you can easily change it right away. To do that properly, activate Xcode first:

1
2
3
tell application "Xcode"
    activate
end tell

Some extras

Two extra functions I found useful:

1
2
3
4
5
6
7
8
9
10
-- Looks for any value from `values_array` in `array` and returns the first match. Otherwise, null.
on find_first_value_in_array(array, values_array)
    repeat with value in values_array
        if array contains value then
            return value
        end if
    end repeat

    return null
end find_first_value_in_array
1
2
3
4
5
6
7
8
to joinList(aList, delimiter)
    set retVal to ""
    set prevDelimiter to AppleScript's text item delimiters
    set AppleScript's text item delimiters to delimiter
    set retVal to aList as string
    set AppleScript's text item delimiters to prevDelimiter
    return retVal
end joinList

(Source: http://geert.vanderkelen.org/splitting-as-string-and-joining-a-list-using-applescript/)

And the last tip for today: if you need to call a function in the global namespace from a tell application … end tell block, prepend the call with the word my.

So long.

Comments