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

AppleScript:如何创建跨脚本访问的永久全局变量并查看已创建变量?

AppleScript 永久全局变量的实现方法

你用的global关键字仅作用于当前脚本会话,脚本运行结束后变量就会被释放,无法跨脚本访问。要实现永久存储且可被任意AppleScript访问的变量,需要依赖外部存储机制,以下是两种常用方案:

方案一:使用 plist 文件(推荐)

macOS系统偏好设置本身就是用plist存储的,这种方法兼容性好、操作简便。

写入/修改变量

-- 定义变量名和值
set varName to "myVariable"
set varValue to "text"

-- 写入到自定义偏好设置 plist(建议用反向域名格式命名,避免冲突)
do shell script "defaults write com.your.custom.vars " & varName & " -string " & quoted form of varValue

读取变量

set varName to "myVariable"

try
    -- 读取变量值
    set myVariable to do shell script "defaults read com.your.custom.vars " & varName
on error
    -- 变量不存在时返回默认值
    set myVariable to ""
end try

log myVariable

方案二:使用文本文件存储

如果需要更灵活的自定义格式,可以用普通文本文件存储变量。

写入/修改变量

set varName to "myVariable"
set varValue to "text"
-- 存储路径:用户目录下的隐藏文本文件
set varsFilePath to (path to home folder as text) & ".applescript_custom_vars.txt"

try
    -- 读取现有内容
    set existingContent to read file varsFilePath
    set AppleScript's text item delimiters to linefeed
    set contentLines to text items of existingContent
    set AppleScript's text item delimiters to ""

    set newLines to {}
    set varExists to false

    -- 遍历行,替换已存在的变量值
    repeat with line in contentLines
        if line starts with (varName & "=") then
            set end of newLines to varName & "=" & varValue
            set varExists to true
        else
            set end of newLines to line
        end if
    end repeat

    -- 变量不存在则追加新行
    if not varExists then
        set end of newLines to varName & "=" & varValue
    end if

    -- 写入新内容
    set AppleScript's text item delimiters to linefeed
    set newContent to newLines as text
    set AppleScript's text item delimiters to ""
    write newContent to file varsFilePath
on error
    -- 文件不存在时创建新文件并写入
    write (varName & "=" & varValue) to file varsFilePath
end try

读取变量

set varName to "myVariable"
set varsFilePath to (path to home folder as text) & ".applescript_custom_vars.txt"

try
    set existingContent to read file varsFilePath
    set AppleScript's text item delimiters to linefeed
    set contentLines to text items of existingContent
    set AppleScript's text item delimiters to ""

    set varValue to ""
    repeat with line in contentLines
        if line starts with (varName & "=") then
            -- 提取等号后的变量值
            set varValue to text ((length of varName) + 2) thru end of line
            exit repeat
        end if
    end repeat

    log varValue
on error
    log "变量不存在或文件未创建"
end try

查看所有已存储的变量

针对 plist 方案

直接读取整个plist的内容:

do shell script "defaults read com.your.custom.vars"

也可以在终端运行相同命令,输出会更格式化。

针对文本文件方案

读取整个文件内容即可:

set varsFilePath to (path to home folder as text) & ".applescript_custom_vars.txt"
try
    set allVars to read file varsFilePath
    log allVars
on error
    log "无存储的变量"
end try

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 02:55:23