gpt4 book ai didi

applescript - 删除字符串 : AppleScript 中前后空格

转载 作者:行者123 更新时间:2023-12-02 21:52:32 26 4
gpt4 key购买 nike

我正在尝试删除字符串中前后的空格,但我使用的代码不起作用...仅当我选择开头或结尾没有空格的目录路径时它仍然有效。我做错了什么?

on run {input, parameters}
set filePath to input

set ASTID to AppleScript's AppleScript's text item delimiters
set AppleScript's text item delimiters to space
set unwantedSpaces to filePath's text items

set a to 1
set b to (count unwantedSpaces)

repeat while (a < b) and ((count item a of unwantedSpaces) is 0)
set a to a + 1
end repeat

repeat while (b > a) and ((count item b of unwantedSpaces) is 0)
set b to b - 1
end repeat

set strippedText to text from text item a to text item b of filePath
set AppleScript's AppleScript's text item delimiters to ASTID

set validFilePaths to {}

repeat with aLine in strippedText
try
set targetAlias to POSIX file aLine as alias
tell application "Finder" to reveal targetAlias
set end of validFilePaths to {}

end try
end repeat
return validFilePaths
end run

最佳答案

类似于 double_j 的 answer ,但对于登陆这里的其他人来说......

我经常使用一个简单的子例程,echo "str1 "| xargs shell 脚本:

on trim(theText)
return (do shell script "echo \"" & theText & "\" | xargs")
end trim

OR 因为 JavaScript 是我的典型语言,所以我有时会在 JS 中思考并执行此操作(尽管它效率非常低,所以我不会将其用于 修剪,但当效率并不重要时,对于更复杂的事情来说,它可以快速获胜):

on trim(theText)
return (do shell script "osascript -l JavaScript -e '\"" & theText & "\".trim()'")
end trim

(也许有一种真正的方法可以在AppleScript中执行内联JS,但我不确定...... yet)

关于applescript - 删除字符串 : AppleScript 中前后空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27304792/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com