从Applescript中的变量中删除单词
我是Applescript的新手,我不知道如何从变量中删除包含“#”的单词 我的脚本收到此错误->“无法将word转换为整型。”从word到整型的数字为-1700 以下是我目前的脚本:从Applescript中的变量中删除单词,applescript,Applescript,我是Applescript的新手,我不知道如何从变量中删除包含“#”的单词 我的脚本收到此错误->“无法将word转换为整型。”从word到整型的数字为-1700 以下是我目前的脚本: activate application "Grids" delay 2 tell application "System Events" keystroke "a" using command down delay 0.25 keystroke "c" using command dow
activate application "Grids"
delay 2
tell application "System Events"
keystroke "a" using command down
delay 0.25
keystroke "c" using command down
delay 0.25
set Description to the clipboard
if any word in Description contains "#" then delete that word
return Description
end tell
有什么建议吗
干杯,
Chris要从剪贴板中取出文本,请使用
(剪贴板作为文本)
。剪贴板几乎可以包含任何内容,甚至可以包含多种格式的多个对象,因此as text
提供了一个字符串供您使用
请注意:“Description”似乎是某些现有appleScript“术语”的一部分,至少在我这里的Mac上是这样,因此我将您的标识符更改为desc
:
activate application "Grids"
delay 2
tell application "System Events"
keystroke "a" using command down
delay 0.25
keystroke "c" using command down
delay 0.25
set desc to the clipboard as text
end tell
set out to {}
set tids to AppleScript's text item delimiters
set AppleScript's text item delimiters to " "
repeat with anItem in (text items of desc)
set str to (anItem as string)
if (str does not contain "#") then
set end of out to str
end if
end repeat
set outStr to out as string
set AppleScript's text item delimiters to tids
return outStr
此代码只返回您要查找的文本。它不会重新插入整理过的字符串,也不会执行任何其他有趣的操作
我假设您要告诉系统事件通过cmd-v粘贴它。(粘贴之前,请记住将剪贴板设置为outtr
)
AppleScript的文本项分隔符
允许使用空格(或您希望的任何其他标记)拆分和重新组合字符串。出于代码卫生的原因,明智的做法是在更改它之前存储它,然后再将其重置为原始值,如图所示,否则脚本中可能会发生奇怪的事情,期望它具有默认值。OP说“如果单词中包含#
”。它没有说“如果单词以#
开头”。因此,当前读取的行:if(str的字符1)不是“#”,那么
应该替换为if(str不包含“#”),然后
谢谢@RobC!更正。AppleScript的文本项分隔符
仅在当前运行脚本的范围内是全局的,如果单独运行,则不会影响单独的独立脚本。AppleScript的文本项分隔符的默认值是{”“}
,如果不是首先更改为开始,则不需要将其保存到变量并还原!如果确实更改了AppleScript的文本项分隔符
,则应在需要更改的适用代码运行后将其重置为默认值,特别是如果要多次运行同一脚本而不首先重新编译它。您也无需使用AppleScript的
,只需文本项分隔符
。换句话说,假设默认值起作用,只需要两条语句。第一个用于更改分隔符,第二个用于将其重置为默认值。例如,将文本项分隔符设置为{“”}
,然后:将文本项分隔符设置为{“”}
是,“…如果它没有更改为带有”和“如果它自己运行”以及“假设…”-确实如此,但事实是,您可能无法确定,因此出于代码卫生的原因,我不愿意假设、存储和重置。这并不难,而且可以使代码更容易重构。提到AppleScript作为文本项定界符的父级
可能是一个难以改掉的旧习惯(旧时代需要IIRC)。我现在已经删除了对全局范围的提及。但没错,AppleScript不适合“严肃”编程,所以quick-n-dirty也可以。如果有效,那就好了。