AppleScript:删除文本字符串中的最后一个字符
AppleScript: remove last character in text string
我需要删除文本字符串 AppleScript 中的最后一个字符。事实证明这是比我想象的要困难得多的问题。
有人可以解释这是如何完成的吗?
试试这个
set t to "this is a string"
text 1 thru -2 of t
输出:
"this is a strin"
同情。这应该是一项微不足道的任务(在大多数语言中,这只是调用内置 "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
您可以考虑购买 AppleScript book(注意:我与人合着了 Apress 的),它涵盖了常见的文本和列表处理任务,比 AppleScript 自己的文档要好得多。
另一种选择是通过 AppleScript-ObjC 桥调用 Cocoa,让您可以免费访问大量现成的功能。它的工作有点多,但对于更高级的文本处理任务,它通常是最简单、最安全和最有效的解决方案。我建议你买一本 Shane Stanley 的 Everyday AppleScript-ObjC if/when 你就这样吧。
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
我觉得这很有趣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
我需要删除文本字符串 AppleScript 中的最后一个字符。事实证明这是比我想象的要困难得多的问题。 有人可以解释这是如何完成的吗?
试试这个
set t to "this is a string"
text 1 thru -2 of t
输出:
"this is a strin"
同情。这应该是一项微不足道的任务(在大多数语言中,这只是调用内置 "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
您可以考虑购买 AppleScript book(注意:我与人合着了 Apress 的),它涵盖了常见的文本和列表处理任务,比 AppleScript 自己的文档要好得多。
另一种选择是通过 AppleScript-ObjC 桥调用 Cocoa,让您可以免费访问大量现成的功能。它的工作有点多,但对于更高级的文本处理任务,它通常是最简单、最安全和最有效的解决方案。我建议你买一本 Shane Stanley 的 Everyday AppleScript-ObjC if/when 你就这样吧。
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
我觉得这很有趣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