AppleScript:删除文本字符串中的最后一个字符
我需要删除文本字符串AppleScript中的最后一个字符。这个问题比我想象的要难得多。 有人能解释一下怎么做吗?试试这个AppleScript:删除文本字符串中的最后一个字符,applescript,Applescript,我需要删除文本字符串AppleScript中的最后一个字符。这个问题比我想象的要难得多。 有人能解释一下怎么做吗?试试这个 set t to "this is a string" text 1 thru -2 of t 输出: “这是一种同情”。这应该是一项微不足道的任务(在大多数语言中,这只是调用一个内置的“trim”函数的问题),但AppleScript拥有绝对糟糕的库支持,而社区本身也没有采取任何措施来填补这些空白,因此,即使是这样的基本日常事务,您也必须使用自己的临时处理程序。例如:
set t to "this is a string"
text 1 thru -2 of t
输出:
“这是一种同情”。这应该是一项微不足道的任务(在大多数语言中,这只是调用一个内置的“trim”函数的问题),但AppleScript拥有绝对糟糕的库支持,而社区本身也没有采取任何措施来填补这些空白,因此,即使是这样的基本日常事务,您也必须使用自己的临时处理程序。例如:
on trimLastChar(theText)
if length of theText = 0 then
error "Can't trim empty text." number -1728
else if length of theText = 1 then
return ""
else
return text 1 thru -2 of theText
end if
end trimLastChar
你可以考虑投资(注:我共同写Apress一),它倾向于覆盖普通文本和列表处理任务,比Apple脚本本身的文档要好得多。
另一种选择是通过AppleScript ObjC桥调用Cocoa,让您可以免费访问大量现成的功能。这需要更多的工作,但对于更高级的文本处理任务,它通常是最简单、最安全、最有效的解决方案。如果你那样做的话,我建议你买一份Shane Stanley的《如果你那样做的话》。我觉得这很有趣
tell application "Safari"
open location "https://www.youtube.com/watch?v=IxnD5AViu6k#t=152.07430805"
delay 1
end tell
delay 1
tell application "Safari"
set theURL to URL of front document as text
end tell
if theURL contains "#" then
repeat until theURL does not contain "#"
set theURL to text 1 thru -2 of theURL
end repeat
end if
基本上,您可以导入Foundation
来帮助您
use scripting additions
use framework "Foundation"
property NSString : a reference to current application's NSString
下面是如何删除最后一个路径组件
on myRemoveLastPath(myPath)
set myString to NSString's stringWithString:myPath
set removedLastPathString to myString's stringByDeletingLastPathComponent
removedLastPathString as text
end myRemoveLastPath
到目前为止,您是否有获取文本字符串的脚本?如果是,请编辑问题以显示当前脚本。