applescript - Tell QuickTime to open a random video file in a folder with AppleScript
问题描述
I would like to play a random video file using QuickTime. My default application for opening video files is VLC, so I specifically want to tell QuickTime Player to open the file, rather than telling Finder to open the file with the default application.
I ran into issues when using 'some file' to select a random file as part of the "tell application "QuickTime Player"" section (I assume it only works with the application "Finder"), so I ended up trying a workaround that involves duplicating a random file to a specific location, then opening that specific location.
My current solution:
tell application "Finder"
set folderPath to "Macintosh HD:Users:username:Desktop:No Backup:Music:TimeOut:"
delete (every item of folder folderPath whose name is "video.mp4")
set sourceFile to some file of folder folderPath
set duplicateFile to duplicate file sourceFile of folderPath
set the name of duplicateFile to "video.mp4"
end tell
tell application "QuickTime Player"
set theMovie to open "Macintosh HD:Users:username:Desktop:No Backup:Music:TimeOut:video.mp4"
tell theMovie
set the looping of theMovie to true
set the present of theMovie to true
play
end tell
end tell
This leads to the following error, highlighting the duplicate file sourceFile of folderPath
bit:
Can’t make «class docf» "One of the random videos.mp4" of «class cfol» "TimeOut" of «class cfol» "Music" of «class cfol» "No Backup" of «class cfol» "Desktop" of «class cfol» "username" of «class cfol» "Users" of «class sdsk» of application "Finder" into type integer.
The QuickTime Player part of the script seems to be working correctly; I'm trying to work out the first half of the script.
A bit of help would be very much appreciated!
解决方案
The following example AppleScript code works for me when the value of the folderPath
variable is a proper HFS path:
set folderPath to ¬
"Macintosh HD:Users:username:Desktop:No Backup:Music:TimeOut:"
tell application "Finder" to ¬
set the fileToPlayInQuickTime to ¬
some file of container folderPath as alias
tell application "QuickTime Player"
set theMovie to open fileToPlayInQuickTime
tell theMovie
set the looping of theMovie to true
set the present of theMovie to true
play
end tell
end tell
If you want to target a specific type of file, assuming there are various types in the target folder, then use the following example AppleScript code while setting the value of the name extension
property to one that is compatible with the target application:
tell application "Finder" to ¬
set the fileToPlayInQuickTime to ¬
(some file of container folderPath ¬
whose name extension is "mp4") as alias
推荐阅读
- symfony - 如何将 current() 传递给 hautelook 灯具中的模板
- c++ - 如何在 C++ 中装饰递归函数
- mule - 如何在 Anypoint Studio 中导入 Mule 连接器?
- c# - 在没有构造函数的类文件(不是组件)中注入依赖服务,在 blazor 中具有某种继承的注入属性?
- typescript - 在 TypeScript 中使用 rest 运算符删除了可选参数
- unity3d - Forge ARKit - 从 Navisworks 到 Unity
- google-analytics - DataLayer 变量/自定义维度仅适用于特定页面(单页应用)
- c++ - 如何将文件存储到包含 C++ 中的类对象的向量中?
- c++ - 将 uint8_t* 传递给 void* 参数函数时丢失数据?
- node.js - Node / Mongo - 增加并获取多个客户端的计数器值