0
votes

I need to insert an AppleScript class name into a list without the quotes so I can enable AirPlay.

i.e. I would like my script to create the following list depending on user interaction: {AirPlay device "Computer", AirPlay device "Apple TV"}

Note the placement of the quotes.

Then I can use this list to turn on AirPlay and stream to the selected devices. e.g. tell iTunes to set current AirPlay devices to AirPlayDevicesSelectedList

Below is my script so far but it does not work because the quotes in the list of devices are in the wrong place!

So how do I insert the class name AirPlay device into a list without the quotes?

I suspect the fix may involve changing the AppleScript delimiters.

tell application "iTunes"
    
    if AirPlay enabled is true then
        display dialog "Would you like to turn off AirPlay?" buttons {"No", "Yes"} default button "yes" with title "AirPlay"
        if button returned of result = "Yes" then
            set current AirPlay devices to {AirPlay device "Computer"}
        end if
    else
        display dialog "Would you like to turn on AirPlay?" buttons {"No", "Yes"} default button "No" with title "AirPlay"
        if button returned of result = "Yes" then
            set airPlayDevices to (get name of every AirPlay device)
            log (airPlayDevices)
            set airPlayDevicesSelected to choose from list airPlayDevices with prompt "Select one or more AirPlay devices" default items {"Computer"} multiple selections allowed yes
            log (airPlayDevicesSelected)
            
            set airPlayDevicesSelectedFix to {}
            log (count of airPlayDevicesSelected)
            repeat with i from 1 to count of airPlayDevicesSelected
                set end of airPlayDevicesSelectedFix to "AirPlay device " & item i of airPlayDevicesSelected
            end repeat
            log (airPlayDevicesSelectedFix)
            set current AirPlay devices to airPlayDevicesSelectedFix
        end if
    end if
end tell    
1
Those terms are enumerations, so they don’t really have text equivalents. The normal way is to use a name or ID to look up the device, for example set end of deviceList to first item of (AirPlay devices whose name is "computer")red_menace
@red-menance -- Many thanks! That code snippet was exactly what I needed to fix my script. I will post the answer with the updated working script.RobK

1 Answers

0
votes

@red_menance provided the answer. One cannot easily insert a class name into an existing list of objects.

In this case, one must use a name or ID of the AirPlay device to look up the device and add the required property to the list using code such as the following:

set end of deviceList to first item of (AirPlay devices whose name is "computer")

Using this technique, I have fixed my script which I have reproduced below:

tell application "iTunes"
    
    if AirPlay enabled is true then
        display dialog "Would you like to turn off AirPlay?" buttons {"No", "Yes"} default button "yes" with title "AirPlay"
        if button returned of result = "Yes" then
            set current AirPlay devices to {AirPlay device "Computer"}
        end if
    else
        display dialog "Would you like to turn on AirPlay?" buttons {"No", "Yes"} default button "No" with title "AirPlay"
        if button returned of result = "Yes" then
            set airPlayDeviceNames to (get name of every AirPlay device whose available is true)
            set airPlayDeviceNamesSelected to choose from list airPlayDeviceNames with prompt "Select one or more AirPlay devices" default items {"Computer"} multiple selections allowed yes
            if airPlayDeviceNamesSelected is not false then
                set airPlayDeviceList to {}
                repeat with apDevice in airPlayDeviceNamesSelected
                    set end of airPlayDeviceList to first item of (AirPlay devices whose name is apDevice)
                end repeat
                set current AirPlay devices to airPlayDeviceList
            end if
        end if
    end if
end tell

Please note: This script is really basic. It will not properly handle two AirPlay devices with the same name (but different id's).