You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AppleScript实现QuickTime录制自动保存遇权限错误求解决

解决QuickTime录制自动保存权限报错问题

问题背景

尝试用AppleScript实现QuickTime录制文件自动保存到指定目录,文件名包含日期和时间。先通过脚本创建带日期的子文件夹,再启动QuickTime录制,停止后保存到新建文件夹,但始终提示无写入权限,环境为QuickTime Player 10.5、MacOS Ventura 13.0.1。

原脚本分为两部分:

创建文件夹脚本

tell application "Finder"
    activate
    set DateStamp to short date string of (current date)
    set patientname to text returned of (display dialog "patient name:" default answer "")
    set folderpath to POSIX path of (choose folder with prompt "choose folder")
    
    do shell script "/bin/mkdir -p " & quoted form of folderpath & "/" & quoted form of (patientname) & "/" & DateStamp
end tell

QuickTime录制脚本

tell application "QuickTime Player"
   activate
        
   repeat
            
   set tdoc to new movie recording -- document "Movie Recording"
            
   say "Ready!"
            
   delay 1
            
   repeat while exists tdoc
    delay 1
   end repeat -- the recording is stopped
            
   say "Stop!"
            
   tell front document
    set TimeStamp to time string of (current date)
    set fileName to POSIX path of folderpath & "/" & patientname & "/" & DateStamp & "/" & patientname & "_" & DateStamp & "_" & TimeStamp & ".mov"
    save it in POSIX file fileName
        close
   end tell
            
   end repeat
    
end tell

核心问题与修复方案

1. 文件名含非法字符

time string返回的格式带有冒号(如14:35:20),而MacOS文件名不允许使用冒号——这是导致"权限错误"的真实原因,系统因文件名非法无法写入,却误报权限问题。需将冒号替换为横杠、点等合法字符。

2. 变量作用域隐患

原脚本中folderpath、patientname、DateStamp定义在Finder的tell块内,虽全局可访问,但跨tell块调用易出现上下文冲突,建议将变量移至所有tell块外定义。

3. 文件夹创建方式不够安全

调用shell的mkdir存在路径转义风险,改用AppleScript原生的Finder创建文件夹,能自动处理权限和路径验证。

4. 保存路径处理错误

原脚本重复对folderpath调用POSIX path of,但folderpath本身已是POSIX路径,多余操作会导致路径错误;保存时需确保路径转换为QuickTime可识别的POSIX file对象。

修复后的完整脚本

-- 全局定义变量,确保跨应用tell块可访问
set currentDate to current date
set DateStamp to short date string of currentDate
set patientname to text returned of (display dialog "patient name:" default answer "")
set parentFolder to choose folder with prompt "choose folder"
set parentFolderPOSIX to POSIX path of parentFolder

-- 用Finder原生方法创建文件夹,避免shell转义问题
tell application "Finder"
    activate
    set patientFolder to make new folder at parentFolder with properties {name:patientname}
    set dateFolder to make new folder at patientFolder with properties {name:DateStamp}
    set saveFolderPOSIX to POSIX path of dateFolder
end tell

-- QuickTime录制与自动保存逻辑
tell application "QuickTime Player"
    activate
    
    repeat
        -- 启动新录制会话
        set recordingDoc to new movie recording
        say "Ready!"
        delay 1
        
        -- 等待录制停止(录制窗口关闭即视为停止)
        repeat while exists recordingDoc
            delay 1
        end repeat
        
        say "Stop!"
        
        -- 处理并保存录制文件
        if exists front document then
            tell front document
                -- 清理时间戳中的非法冒号
                set TimeStamp to time string of (current date)
                set cleanedTimeStamp to replaceText(TimeStamp, ":", "-")
                -- 生成合法文件名
                set fileName to saveFolderPOSIX & patientname & "_" & DateStamp & "_" & cleanedTimeStamp & ".mov"
                -- 保存并关闭文件
                save it in POSIX file fileName
                close
            end tell
        end if
    end repeat
end tell

-- 字符串替换辅助函数,处理非法字符
on replaceText(theText, searchString, replacementString)
    set AppleScript's text item delimiters to searchString
    set theTextItems to every text item of theText
    set AppleScript's text item delimiters to replacementString
    set theText to theTextItems as string
    set AppleScript's text item delimiters to ""
    return theText
end replaceText

额外提示

  • 首次运行脚本时,系统会请求Finder和QuickTime的权限,需点击"允许"以确保脚本能访问文件系统并控制应用。
  • 脚本中的repeat循环会持续生成新的录制窗口,可通过终止脚本或退出QuickTime停止循环。
  • 若需要固定格式的日期(如YYYY-MM-DD),可替换DateStamp的生成逻辑:
    set DateStamp to (year of currentDate) & "-" & text -2 thru -1 of ("0" & (month of currentDate as integer)) & "-" & text -2 thru -1 of ("0" & (day of currentDate))
    

内容的提问来源于stack exchange,提问作者paffi05

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 09:25:44