我需要在一个文本字符串中使用AppleScript删除最后一个字符。这比我想象的要困难得多。 有人能解释一下如何实现吗?
我需要在一个文本字符串中使用AppleScript删除最后一个字符。这比我想象的要困难得多。 有人能解释一下如何实现吗?
试试这个
set t to "this is a string"
text 1 thru -2 of t
输出:
“这是一个字符串”
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
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
repeat while theURL contains "#"
set theURL to text 1 thru -2 of theURL
end repeat
我觉得这很有趣 http://applehelpwriter.com/2016/09/06/applescript-remove-characters-from-a-string/
基本上,你可以导入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