如何从字符串获取/设置applescript属性?
我正试图用applescript在MacOS中自动定位我的dock 我可以成功定位码头。这非常有效:如何从字符串获取/设置applescript属性?,applescript,Applescript,我正试图用applescript在MacOS中自动定位我的dock 我可以成功定位码头。这非常有效: tell application "System Events" tell dock preferences set properties to {screen edge:right} end tell end tell 问题是我想接受位置作为参数,它是作为字符串提供的。因此,我得出的结论相当于: tell application "System Events" tell doc
tell application "System Events"
tell dock preferences
set properties to {screen edge:right}
end tell
end tell
问题是我想接受位置作为参数,它是作为字符串提供的。因此,我得出的结论相当于:
tell application "System Events"
tell dock preferences
set x to "right"
set properties to {screen edge:x}
end tell
end tell
这将导致一个错误:
“系统事件出错:无法\U2019t将\“正确\”转换为类型常量。”;
如何将字符串“解析”为所需的常量?right
(不带引号)是一个整数值,一个枚举常量。不能将字符串强制转换为枚举
如果您真的需要一个字符串参数,那么解决方法是一个If-else
链
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
然后可以使用字符串参数更改边
setDockScreenEdge("right")
无需设置属性
记录。您可以直接设置单个属性right
(不带引号)是一个整数值,一个枚举常量。不能将字符串强制转换为枚举
如果您真的需要一个字符串参数,那么解决方法是一个If-else
链
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
然后可以使用字符串参数更改边
setDockScreenEdge("right")
无需设置
属性
记录。您可以直接设置单个属性如果我知道常量是什么,我可以将参数作为整数提供。我怎么知道呢?枚举常量是事实上的整数,但被封装,不能用普通整数表示。如果我知道常量是什么,我可以提供一个整数参数。我怎么知道呢?枚举常量是事实上的整数,但被封装,不能用普通整数表示。