打开文件夹时播放声音(AppleScript)

时间:2017-03-28 01:49:40

标签: applescript

对此很新。

我正在尝试弄清楚如何在没有打开任何窗口或使用任何外部应用程序的情况下打开文件夹时播放音频文件。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

您正在寻找的常规技术称为“文件夹操作” - 这些是可编写脚本的操作,可在您打开/关闭文件夹或创建/删除文件时触发。

选择以下Applescript并将其复制为 C

on opening folder this_folder
    do shell script "afplay '/System/Library/Sounds/Hero.aiff'"
end opening folder
  • 现在通过输入 + (即Script EditorCmd)开始SPACEBAR并开始输入Script Editor。一旦猜到你的意思,点击 Enter

  • 使用 V 将上述脚本粘贴到编辑器中,然后转到File - > Save,然后导航到$HOME/Library/Scripts/Folder Actions Scripts并将脚本保存为脚本包,并将其命名为SoundOnOpen。如果您以前从未这样做,则可能必须创建Folder Actions Scripts文件夹。如果您在Finder中看不到Library文件夹,请点击 G 并在框中输入$HOME/Library点击输入

  • 现在,在桌面上创建一个文件夹,将其命名为Musical并右键单击它。将鼠标悬停在Services上并左键单击Folder Actions Setup...,然后将我们在上面创建的脚本分配到此文件夹。

在Finder中打开文件夹,它会发出声音!

答案 1 :(得分:0)

如果您想让自己的生活更轻松一些,我建议您创建一个新的脚本编辑器文件,允许您选择要在文件夹操作中使用的音频文件。在这个新的AppleScript文档中,只需输入以下代码:

choose file
单击

,然后单击“运行”按钮。这将打开一个文件选择器窗口,允许您转到计算机上的任何目录并选择您要使用的任何音频文件。

enter image description here

选择文件后,从结果窗口复制整个文本。现在是时候创建新的文件夹操作了。创建另一个新的脚本编辑器文档,并将光标放在文档的顶部,然后按Ctrl +单击

enter image description here

enter image description here

现在请记住,您从“选择文件”脚本的结果窗口中复制了该文件信息。将该信息粘贴到您的代码中,如我在示例图像中所见。

enter image description here

on opening folder this_folder
    set a to alias "Macintosh HD:Users:Smokestack:Music:iTunes:iTunes Media:Music:Pink Floyd:Dark Side Of The Moon - 30th Anniversary SACD:09 Brain Damage.mp3"
    set p to POSIX path of a
    do shell script "afplay -t 25 " & quoted form of p
    (* The "-t 25" Is just an option telling afplay shell script to only play the audio for 25 seconds.  You can adjust that number to what ever you would like or remove that part of the command completely.
    the code below this line is what you would use if you don't want a time limit on the audio
    do shell script "afplay " & quoted form of p  *)
end opening folder

现在编译并将脚本保存到/ Users / YOUR_SHORT_USER_NAME / Library / Workflows / Applications / Folder Actions

您要做的下一件事是打开您可以在此处找到的Folder Actions Setup.app:/ System / Library / CoreServices / Applications / Folder Actions Setup.app

因为您将AppleScript文件夹操作文件保存到该特定位置,所以当您选择“将脚本附加到文件夹”时,它现在可在您的文件夹操作Setup.app中使用

enter image description here

enter image description here