删除string:AppleScript中前面和后面的空格
我试图删除字符串中的前后空格,但我使用的代码不起作用。。。仅当我选择一个在开始或结束处没有空格的目录路径时,它仍然有效。我做错了什么删除string:AppleScript中前面和后面的空格,applescript,Applescript,我试图删除字符串中的前后空格,但我使用的代码不起作用。。。仅当我选择一个在开始或结束处没有空格的目录路径时,它仍然有效。我做错了什么 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 unw
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
运行时{input,parameters}
将文件路径设置为输入
将ASTID设置为AppleScript的AppleScript的文本项分隔符
将AppleScript的文本项分隔符设置为空格
将unwantedSpaces设置为文件路径的文本项
将a设置为1
将b设置为(计数未分配的空间)
当(aa)和((未命名空间的计数项b)为0时重复此操作)
将b设置为b-1
结束重复
将strippedText设置为文本,从filePath的文本项a设置为文本项b
将AppleScript的AppleScript文本项分隔符设置为ASTID
将ValidFilePath设置为{}
在strippedText中用aLine重复
尝试
将targetAlias设置为POSIX文件aLine作为别名
告诉应用程序“Finder”显示targetAlias
将ValidFilePath的结尾设置为{}
结束尝试
结束重复
返回有效路径
终点
如果您希望从字符串中修剪空格,下面的脚本应该可以做到这一点。但是,您的变量名filePath使我相信您的输入是一个文件路径;)。如果是这种情况,则不会因为名称扩展名而修剪路径的结尾,也不会修剪路径的开头,除非驱动器的名称以空格开头。如果要修剪文件名中的空格,则必须调整脚本
on run {input, parameters}
set filePath to (input as text)
return my trim(filePath, true)
end run
-- From Nigel Garvey's CSV-to-list converter
-- http://macscripter.net/viewtopic.php?pid=125444#p125444
on trim(txt, trimming)
if (trimming) then
repeat with i from 1 to (count txt) - 1
if (txt begins with space) then
set txt to text 2 thru -1 of txt
else
exit repeat
end if
end repeat
repeat with i from 1 to (count txt) - 1
if (txt ends with space) then
set txt to text 1 thru -2 of txt
else
exit repeat
end if
end repeat
if (txt is space) then set txt to ""
end if
return txt
end trim
下面是另一个更简单的方法:
on run {input, parameters}
set filePath to input as text
set Trimmed_filePath to do shell script "echo " & quoted form of filePath & " | sed -e 's/^[ ]*//' | sed -e 's/[ ]*$//'"
end run
类似于double_j,但对于其他在这里着陆的人
我经常使用一个简单的子程序,带有echo“str1”| xargs
shell脚本:
on trim(theText)
return (do shell script "echo \"" & theText & "\" | xargs")
end trim
或因为JavaScript是我的典型语言,我有时会在JS中思考并这样做(尽管它效率极低,所以我不会将其用于修剪
,但在效率不重要的情况下,它可以快速解决更复杂的问题):
(也许有一种真正的方法可以在AppleScript中执行内联JS,但我不确定……)这是一个文件路径,但它是一个已复制并粘贴到word文档或电子邮件中的文件路径。这个想法是,有人可以选择路径,右键单击,选择服务,然后选择这个applescript。然后,该脚本将获取所选字符串并在Finder中定位该文件。但是我注意到,如果我三次单击字符串并选择整行,而不仅仅是字符串本身,由于额外的空字符/空格,它将无法工作。此脚本是否适用于此场景?它需要单独保留文件路径中的空格。许多人并不觉得正则表达式更简单。此外,如果使用-e,则不需要管道输出。将endString设置为执行shell脚本“sed-Ee's/^[]+/'-e's/[]+$/'@adayzdone我很感谢你的反馈。我还是会用我的,因为它大约是代码的十分之一。而且似乎效率更高。它的代码更少……但效率/速度没有那么快。@adayzdone我印象深刻。快了20毫秒,+1:)我不得不说,如果这是一个大文件或列表,那么sed和或perl必须更快,对吗?我是int我已经看到结果了。更新变量并让我知道。
on trim(theText)
return (do shell script "osascript -l JavaScript -e '\"" & theText & "\".trim()'")
end trim