简单的 Applescript - Quicktime 问题播放歌曲 [英] Simple Applescript - Quicktime Issue Play Song

查看:34
本文介绍了简单的 Applescript - Quicktime 问题播放歌曲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Applescript 做一件非常基本的事情,但我遇到了麻烦.我正在尝试创建一个非常简单的 Applescript,以便使用 QuickTime Player 在我的桌面上打开一个普通的 MP3 文件.

I'm trying to do a very basic thing using Applescript but I'm having trouble. I am trying to create a very simple Applescript to just open a plain vanilla MP3 file on my desktop with QuickTime Player.

我想使用 QuickTime 并将其打开以便用户可以控制它等

I want to use QuickTime and have it open so user can control it etc.

基本问题是文件名或文件夹路径似乎有时会随机引起问题,尤其是在名称或路径文件夹名称等中有空格时.

The basic problem is that the file name or folder path seems to be sometimes randomly causing problems especially when it has spaces in the name or the path folder names etc.

如果我双击任何文件,它们都会在 QuickTime 中打开.

If I double click on any file they all open in QuickTime.

我创建了一个非常基本的短音频文件,并用几个不同的名称保存它,例如:

I created a very basic short audio file and saved it with a few different names like:

test1.mp3
测试 1.mp3
这是一个测试.mp3

test1.mp3
test 1.mp3
this is a test.mp3

然后我在下面创建了这个非常简单的 Applescript.

Then I created this very simple Applescript below.

通常= test1.mp3 几乎总是会打开并开始播放,但是标题中有空格的其他版本经常会产生和错误(我认为打开歌曲行失败),例如:

generally = test1.mp3 will almost always open and start playing, however the other Versions with spaces in the title often produce and error (open thesong line fails i think) like:

无法打开文件test1.mp3".该文件与 QuickTime Player 不兼容.

The document "test1.mp3" could not be opened. The file isn’t compatible with QuickTime Player.

错误QuickTime Player 出现错误:无法获取文档 1.索引无效."来自文档 1 的编号 -1719

error "QuickTime Player got an error: Can’t get document 1. Invalid index." number -1719 from document 1

认为这是因为它无法打开文件.任何帮助将不胜感激.

think this is because it can't open the file. Any help would be appreciated.

Applescript ...

Applescript ...

tell application "Finder"
    
    --varied attempts like this...
    set thesong to "drive:Users:me:Desktop:music:test1.mp3" as string
    set thesong to "drive:Users:me:Desktop:music:test1.mp3"
    set thesong to "drive:Users:me:Desktop:music:this is an audio test.mp3" as string
    set thesong to "drive:Users:me:Desktop:test1.mp3" as string

end tell

tell application "QuickTime Player"
    activate
    open thesong
    play document 1
end tell

我还尝试添加延迟,认为 QuickTime 在打开/播放歌曲之前需要时间打开 - 但仍然有随机失败......

Also I tried adding delays thinking that QuickTime needed time to open before open / play song - but still have random fails...

tell application "QuickTime Player"
activate
delay 3
open thesong
delay 3
play document 1
end tell

推荐答案

在 AppleScript 中,stringPOSIX 文件 («class furl») 之间存在差异, 和一个别名.

In AppleScript, there are differences between a string, a POSIX file («class furl») , and an alias.

当你将歌曲设置为drive:Users:me:Desktop:music:test1.mp3"时as string 创建一个字符串,它只是纯文本.QuickTime Player 的 open 命令需要 POSIX 文件alias,这就是为什么您会收到错误消息(它不知道要做什么)使用纯文本字符串).

When you do set thesong to "drive:Users:me:Desktop:music:test1.mp3" as string that creates a string, which is just plain text. QuickTime Player's open command requires either a POSIX file or an alias, which is why you get an error message (it doesn't know what to do with a plain text string).

虽然您最终需要以 POSIX 文件alias 结束,但您可以从一个字符串开始,然后将其强制转换为您想要的类型.

While you ultimately need to end up with a POSIX file or alias, you can start with a string and then coerce it to the type you want.

下面的任何一个打开命令都应该起作用:

Either of the open commands below should work:

   set posixFile to "/Users/you/Desktop/file.mp3" as POSIX file
   set aliasFile to ":Users:you:Desktop:file.mp3" as alias

   tell app "QuickTime Player"
       set theDoc to open posixFile
       -- or set theDoc to open aliasFile
       play theDoc
   end tell

这篇关于简单的 Applescript - Quicktime 问题播放歌曲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆