扩展带有前缀“~"的 POSIX 路径;(使用 AppleScript) [英] Expand a POSIX path with prefix "~" (using AppleScript)
问题描述
StandardAdditions
有 path to
命令,我们可以使用它来获取已知位置.
StandardAdditions
has the path to
command and we can use it to get known locations.
例如,path to home folder
返回对user folder
的文件引用,如果我们想要posix path
,我们可以这样做((主文件夹的路径)作为文本)的 POSIX 路径
.
For example, path to home folder
returns a file reference to the user folder
and if we want the posix path
, we can do POSIX path of ((path to home folder) as text)
.
在Terminal.app
中,我们可以使用波浪号(~
)来表示home文件夹
.
In Terminal.app
we can use the tilde character (~
) to represent the home folder
.
我们如何在 AppleScript
中执行此 波浪号扩展
?
How can we do this tilde expanding
in AppleScript
?
推荐答案
从 OS X 10.10
开始,您可以轻松访问 Cocoa 类
及其功能和属性:>
Started with OS X 10.10
you can easily access Cocoa classes
and their functions and properties:
use framework "Foundation"
expandTilde("~/Desktop")
on expandTilde(givenPath)
-- create a temporary Obj-C/Cocoa NSString object with the givenPath
set tempCocoaString to current application's NSString's stringWithString:givenPath
-- call the object's stringByExpandingTildeInPath method
-- to create a new path with expanded tilde
return (tempCocoaString's stringByExpandingTildeInPath) as string
end expandTilde
在 10.9
中,您必须在 Scripting Libraries
中定义这样的处理程序,这并不像听起来那么好.但在 10.10
中,这是开箱即用的!
In 10.9
you have to define such handlers in Scripting Libraries
, that's less nice than it sounds. But in 10.10
this works out of the box!
这篇关于扩展带有前缀“~"的 POSIX 路径;(使用 AppleScript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!