播放声音使用afplay和相对路径的AppleScript [英] Play sound with AppleScript using afplay and relative file path
问题描述
我试图创建使用afplay的AppleScript(如建议这里)玩的就是位于目录相同的目录中内的随机声音文件。
文件夹
- applescript.scpt
- 声音
-----声音x.aiff
我发现<一个href=\"http://stackoverflow.com/questions/24152552/relative-file-paths-with-applescript#comment37312009_24152885\">this评论就相对路径是潜在有用的:
(的(POSIX路径路径我))
不过,我不断收到错误,当我尝试用<一糖化起来href=\"http://stackoverflow.com/questions/12377927/how-to-open-multiple-random-files-with-applescript\">this随机性方法 ...
设置数量写3集theFiles到{}
重复
设置为FILE_PATH(中(路径我POSIX路径))的形式引用
告诉应用程序发现者,以å文件设置为(FILE_PATH和放大器的某些文件;/ sounds_dir)作为文本
如果å文件不在theFiles然后
theFiles到å文件的设置结束
告诉应用程序发现者打开文件å文件
做shell脚本(afplay&放大器; FILE_PATH和放大器;&GT;的/ dev / null的2 - ;&放大器; 1安培;)
万一
如果(theFiles计数)等于数量写,然后退出重复
重复结束
在剧本
,路径我
返回路径
到脚本文件
,但我们的需求的其父路径
添加子路径
。
要编写正确的路径,我们可以使用子程序<一个href=\"http://stackoverflow.com/questions/29953091/how-to-customise-notification-in-os-x-using-applescript/29963612#29963612\"><$c$c>composeFilePath(fileName$c$c>).
的主要行动是在重复循环。我还添加了延迟
,以便更容易进行测试。 是,因为路径我之前保存脚本
将返回一个错误的道路时,其保存的。
组回路3
设置soundFolderName为sounds_dir#---------------------
# 建立
#---------------------
设置soundFolderFullPath我composeFilePath(soundFolderName)告诉应用程序发现者 如果再夹soundFolderFullPath存在 设置soundFolder为(文件夹soundFolderFullPath) 其他
设置soundFolder为
#自定义操作时文件夹不存在
嘟
登录***错误:文件夹&放大器;引述soundFolderFullPath和放大器形式; 失踪!
返回
万一 如果(计数soundFolder文件)为0,则
#自定义操作时的文件夹中有没有项目
嘟
登录***错误:在没有项目&放大器;引述soundFolderFullPath和放大器形式; !
返回
万一告诉结束#-------------------------------------------
#我们有soundFolder,它有它的项目
#-------------------------------------------重复循环时间 #PLAY随机文件 #作为单行:
##做shell脚本的/ usr / bin中/ afplay&放大器; &AMP(((soundFolder的一些文件)作为文本)POSIX路径)的报价表; &GT;的/ dev / null的2 - ;&放大器; 1安培; # 一步步
设置aRandomFile为soundFolder的某些文件
设置aRandomFile以POSIX路径(aRandomFile文本) 设置shellscript里为在/ usr /斌/ afplay&放大器;引述aRandomFile和放大器形式; &GT;的/ dev / null的2 - ;&放大器; 1安培;
做shell脚本shellscript里 延迟1重复结束
在composeFilePath(文件名)
如果文件名是,然后返回
设置pathToMe为路径,我 - 这是这个脚本的完整路径
- 得到这个脚本是在文件夹:
thisScriptsFolder设置为
告诉应用程序发现者
尝试
设置thisScriptsFolder以(获取pathToMe的容器)文本
年底试
告诉结束
如果thisScriptsFolder是,那么
返回
万一
返回thisScriptsFolder&安培;文件名 - 完整路径
结束composeFilePath
I'm trying to create an AppleScript that uses afplay (as suggested here) to play a random sound file that's located in a directory within the same directory as the script.
folder
-- applescript.scpt
-- sounds
----- sound-x.aiff
I found this comment regarding relative paths to be potentially useful:
(POSIX path of (path to me))
However, I keep receiving errors when I try mashing it up with this approach for randomness...
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
In a script
, path to me
returns the path
to the script file
but we need its parent path
to add a sub path
.
To compose the correct path, we can use the subroutine composeFilePath(fileName
).
The main action is in the repeat loop. I also added a delay
so it's easier to test. Save the script before using since path to me
will return a wrong path when its unsaved.
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
这篇关于播放声音使用afplay和相对路径的AppleScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!