Matt
Matt

Reputation: 2273

How to get/set applescript property from string?

I'm trying to automate the position of my dock in MacOS with applescript.

I can successfully position the dock. This works perfectly:

tell application "System Events"
 tell dock preferences
   set properties to {screen edge:right}
  end tell
end tell

The problem is that I want to accept the position as an argument, and it's provided as a string. So I end up with the equivalent of:

tell application "System Events"
 tell dock preferences
   set x to "right"
   set properties to {screen edge:x}
  end tell
end tell

This results in an error:

"System Events got an error: Can\U2019t make \"right\" into type constant.";

How do I 'resolve' my string into the constant that is expected?

Upvotes: 1

Views: 485

Answers (1)

vadian
vadian

Reputation: 285069

right (without quotes) is an integer value, an enumerated constant. You cannot cast a string to an enum.

If you really need a string argument a workaround is an if - else chain

on setDockScreenEdge(theEdge)

    tell application "System Events"
        tell dock preferences
            if theEdge is "right" then
                set screen edge to right
            else if theEdge is "left" then
                set screen edge to left
            else if theEdge is "bottom" then
                set screen edge to bottom
            end if
        end tell
    end tell

end setDockScreenEdge

Then you can change the edge with a string argument

setDockScreenEdge("right")

It's not necessary to set the properties record. You can set a single property directly

Upvotes: 2

Related Questions