StandardAdditions
有path to
命令,我们可以使用它来获取已知位置。
例如,path to home folder
会返回user folder
的文件引用,如果我们想要posix path
,我们可以POSIX path of ((path to home folder) as text)
。
在Terminal.app
中,我们可以使用代字号(~
)来表示home folder
。
我们如何在tilde expanding
中执行此操作AppleScript
?
答案 0 :(得分:5)
从OS X 10.10
开始,您可以轻松访问Cocoa classes
及其功能和属性:
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
中,这开箱即用!
答案 1 :(得分:1)
答案 2 :(得分:0)
更多即将推出
如何展开可以
posix path
字符tilde
开头的~
? (~
代表home folder
)
这里是脚本+ demo:
#
demo()
# ------------------------------------------------------------
#
# expandTildeInPath ( localPath, outputStyle )
#
# ------------------------------------------------------------
# When outputStyle is true : returns posix path
# When outputStyle is false : returns alias
# ------------------------------------------------------------
# localPath format:
# ------------------------------------------------------------
# • posix path - a posix path (can start with a tilde "~")
# • alias
# • path (text) with ":" => (alias as text) produces this
# ------------------------------------------------------------
on expandTildeInPath(localPath, outputStyle)
# ------------------------------------------------------------
# If necessary, convert localPath to text and posix path:
# ------------------------------------------------------------
if (class of localPath is alias) then
# alias
set localPath to POSIX path of (localPath as text)
else if (class of localPath is text) and (localPath contains ":") then
# (fileAlias as text) produces such a colon delimited path:
set localPath to POSIX path of localPath
end if
set resultPath to localPath
# -------------------------------
# If necessary, expand tilde:
# -------------------------------
if localPath starts with "~" then
# input is like: "~", "~/" or something with a sub path, like "~/Movies"
set homeFolder to POSIX path of ((path to home folder) as text)
if (localPath = "~") or (localPath = "~/") then
# no sub-path
set resultPath to homeFolder
else
if not (localPath starts with "~/") then
# this is not a valid path to expand but an item that starts with "~"
return localPath
end if
# add sub-path
set subPath to ((characters 3 thru -1) of aPath) as text
set sDel to "/"
if (subPath starts with sDel) then set sDel to ""
set resultPath to homeFolder & sDel & subPath
end if
end if
# -------------------------------
# return posix path or alias:
# -------------------------------
if outputStyle then
return (POSIX path of resultPath)
else
# the location must exist or "as alias" throws an error.
try
set a to (resultPath as POSIX file) as alias
return a
on error
log "*** Error ***" & resultPath & " does not exist"
return localPath
end try
end if
end expandTildeInPath
# ------------------------------------------------------------
# Sub Routines for Demo
# ------------------------------------------------------------
on demo()
set asAlias to false
set asPosixPath to true
# Does not expand since its a name that starts with "~":
logDemo("~testFile", asAlias)
# FolderThatDoesNotExist:
logDemo("~/FolderThatDoesNotExist", asAlias) # this throws an error (in the log) and returns the unchanged input
logDemo("~/FolderThatDoesNotExist", asPosixPath) # returns expanded posix path (existing or not)
# Main Usage
logDemo("~", asPosixPath)
logDemo("~", asAlias)
logDemo("~/", asPosixPath)
logDemo("~/", asAlias)
logDemo("~/Documents/", asPosixPath)
logDemo("~/Documents/", asAlias)
# convert a posix path like "/Applications" to alias
logDemo("/Applications", asAlias)
set input to "~/Music/iTunes/iTunes Library.xml"
set filePosixPath to expandTildeInPath(input, asPosixPath)
set fileAlias to expandTildeInPath(input, asAlias)
log quoted form of input
log quoted form of filePosixPath
log fileAlias
logLine()
# Using alias with Finder is so easy:
tell application "Finder"
if exists fileAlias then
# activate
# reveal fileAlias
end if
end tell
# --------------------------
# Other input formats:
# --------------------------
# This is also possible (not intended but it works):
# we can use text paths (containing ":"):
logDemo((path to documents folder as text), false)
# we can use aliases:
# (makes no sense at all when we return an alias but this is to show that it works)
logDemo((path to documents folder), false)
# Same as above but output as posix path:
logDemo((path to documents folder as text), true)
logDemo((path to documents folder), true)
end demo
on logLine()
log "---------------------------------------------"
end logLine
on logDemo(inputPath, outputStyle)
set a to expandTildeInPath(inputPath, outputStyle)
if outputStyle then
log "input: " & inputPath & " output: " & quoted form of a
else
log "input: " & inputPath & " output (next line): "
log a # don't mix with text or it does not show the "alias" in the log
end if
logLine()
end logDemo
# ------------------------------------------------------------
这里有一点显示系统属性:
set saList to system attribute
repeat with sa in saList
log quoted form of sa & " = " & quoted form of (system attribute sa)
end repeat
答案 3 :(得分:0)
这是一个方便的applescript单行代码,可解析任何以波浪号开头的posix路径(包括示例路径。)之后,您将发现示例转换为其他路径格式。
set psxPTH to "~/Library/Scripts/"
if character 1 of psxPTH contains "~" then set psxPTH to POSIX path of (path to home folder) & text (2 + (offset of "~" in psxPTH)) thru -1 of psxPTH
------>"/Users/Mr.Science/Library/Scripts/"
-posix解决后的其他步骤...
set hfsPTH to (POSIX file psxPTH) as text
------> "Macintosh_Harddrive:Users:Mr.Science:Library:Scripts:"
set alsPTH to alias hfsPTH
------> alias "Macintosh_Harddrive:Users:Mr.Science:Library:Scripts:"
tell application "Finder" to set vrbspth to item alsPTH
------> folder "Scripts" of folder "Library" of folder "Mr.Science" of folder "Users" of startup disk of application "Finder"