我试图创建一个使用afplay(如一个AppleScript 建议在这里 ),以Play发行坐落在一个目录下的同一目录脚本内的随机声音文件。
folder
-- applescript.scpt
-- sounds
----- sound-x.aiff
我发现这个评论就相对路径是潜在有用:
(POSIX path of (path to me))
不过,我不断收到错误,当我尝试混搭起来这种方法随机性 ...
set theNumber to 3
set theFiles to {}
repeat
set file_path to quoted form of (POSIX path of (path to me))
tell application "Finder" to set aFile to (some file of file_path & "/sounds_dir") as text
if aFile is not in theFiles then
set end of theFiles to aFile
tell application "Finder" to open file aFile
do shell script ("afplay " & file_path & " > /dev/null 2>&1 &")
end if
if (count of theFiles) is equal to theNumber then exit repeat
end repeat
在script
, path to me
返回path
的script file
,但我们需要它的parent path
添加一个sub path
。
要撰写正确的路径,我们可以使用子程序composeFilePath(fileName
) 。
其主要作用是在重复循环。 我还添加了一个delay
,以便更容易进行测试。 因为使用之前保存脚本 path to me
,当其未保存会返回一个错误的道路。
set LOOPS to 3
set soundFolderName to "sounds_dir"
# ---------------------
# SETUP
# ---------------------
set soundFolderFullPath to my composeFilePath(soundFolderName)
tell application "Finder"
if folder soundFolderFullPath exists then
set soundFolder to (folder soundFolderFullPath)
else
set soundFolder to ""
# Customize action when folder does not exist
beep
log "*** Error: folder " & quoted form of soundFolderFullPath & " missing!"
return
end if
if (count files in soundFolder) is 0 then
# Customize action when folder has no items in it
beep
log "*** Error: No items in " & quoted form of soundFolderFullPath & " !"
return
end if
end tell
# -------------------------------------------
# We have "soundFolder" and it has items in it
# -------------------------------------------
repeat LOOPS times
# PLAY RANDOM FILE
# As ONE LINER:
## do shell script "/usr/bin/afplay " & quoted form of (POSIX path of ((some file of soundFolder) as text)) & " > /dev/null 2>&1 &"
# Step By Step
set aRandomFile to some file of soundFolder
set aRandomFile to POSIX path of (aRandomFile as text)
set shellScript to "/usr/bin/afplay " & quoted form of aRandomFile & " > /dev/null 2>&1 &"
do shell script shellScript
delay 1
end repeat
on composeFilePath(fileName)
if fileName is "" then return ""
set pathToMe to path to me -- this is the full path to this script
-- get the folder this script is in:
set thisScriptsFolder to ""
tell application "Finder"
try
set thisScriptsFolder to (get container of pathToMe) as text
end try
end tell
if thisScriptsFolder is "" then
return ""
end if
return thisScriptsFolder & fileName -- full path
end composeFilePath
你有你的路全乱了。 所以你不能说类型的路径传递给应用程序,如搜索的AppleScript不使用“POSIX路径”。 Shell脚本做使用POSIX路径,你必须说出来了。
另外对于AppleScript的,只要你的路径字符串格式,你必须把单词“文件”或“文件夹”的路径前在使用它之前,这样的AppleScript可以知道这是在其他方式的AppleScript要采取行动会把它的对象像一个字符串。 在AppleScript的也串路径应当冒号分隔(:)而非斜线分隔(/)。 最后AppleScript的路径开始与您的硬盘驱动器,如“Macintosh硬盘”的名称。
你可以看到,你必须得多不同于你用贝壳做对待AppleScript的路径。 反正,我没有测试此代码,但使用这些基本规则这应该工作。 祝好运。
set theNumber to 3
set theFiles to {}
repeat
set file_path to (path to me) as text
set file_dir to file_path & ":sounds_dir:"
tell application "Finder" to set aFile to (some file of folder file_dir) as text
if aFile is not in theFiles then
set end of theFiles to aFile
tell application "Finder" to open file aFile
do shell script "afplay " & quoted form of POSIX path of aFile & " > /dev/null 2>&1 &"
end if
if (count of theFiles) is equal to theNumber then exit repeat
end repeat