【问题标题】:Play sound when opening folder (AppleScript)打开文件夹时播放声音(AppleScript)
【发布时间】:2017-03-28 01:49:40
【问题描述】:

对此很陌生。

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

【问题讨论】:

  • 您的意思是在 Finder 中打开一个文件夹吗?如果是这样,请更新您的问题以这样说。

标签: applescript


【解决方案1】:

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

选择下面一段 Applescript 并用 C 复制它。

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

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

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

在 Finder 中打开文件夹,它会爆炸!

【讨论】:

    【解决方案2】:

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

    choose file
    

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

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


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

    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

    接下来您要做的是打开文件夹操作 Setup.app,您可以在此处找到它:/System/Library/CoreServices/Applications/Folder Actions Setup.app

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

    【讨论】:

    • 不知道你为什么说我的脚本会出现包含空格的音乐文件的问题 - 文件名正确地用单引号括起来并且即使我播放的文件名称中有空格也可以正常工作。
    • 我的错……在你提到你的单引号后我才注意到。我会修改我的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-24
    • 1970-01-01
    • 2012-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多