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

在Extendscript(JSX)中执行curl AppleScript遇超时问题求助

问题分析与解决

你的代码存在两个核心问题,导致下载超时:

1. AppleScript代码拼接语法错误

原代码中拼接的asCode存在引号不匹配问题,生成的.applescript文件语法错误,无法被正确解析执行。

2. .applescript文件执行方式错误

Photoshop的File.execute()方法仅适用于执行可执行文件(如编译后的.app或二进制程序),纯文本的.applescript脚本需要通过osascript命令调用执行,直接用execute()会导致脚本未实际运行,最终触发超时。


推荐修复方案(直接执行curl,跳过中间脚本文件)

无需生成AppleScript文件,直接在JSX中通过app.doScript()调用AppleScript执行curl,简化逻辑同时避免文件IO问题:

var getMacImage = function(exporturl, assetFolderPath, fileName) {
    try {
        var ff = new File(assetFolderPath + '/' + fileName);
        if (!ff.exists) {
            // 转义路径和URL中的单引号,避免shell解析错误
            var escapedPath = ff.fsName.replace(/'/g, "'\\''");
            var escapedUrl = exporturl.replace(/'/g, "'\\''");
            // 构造AppleShell命令并执行
            var asCommand = 'do shell script \'curl -L -o \'' + escapedPath + '\' \'' + escapedUrl + '\'';
            app.doScript(asCommand, ScriptLanguage.APPLESCRIPT_LANGUAGE);
        }
        // 等待文件下载完成
        var c = 0;
        while(!ff.exists) {
            $.sleep(10); // 调整为10ms间隔,平衡等待精度与资源占用
            c++;
            if (c > 1000) { // 10秒超时阈值
                throw new Error("图片下载超时(超过10秒)");
            }
        }
        return ff;
    }
    catch(e) {
        throw new Error("从" + exporturl + "下载资源失败:" + e.message);
    }
};

var unitTest = function() {
    var afp = Folder.desktop.fsName;
    var imgp = "https://www.google.co.in/intl/en_com/images/srpr/logo1w.png";
    try {
        var f = getMacImage(imgp, afp, "test1.jpg");
        alert(f.exists ? "下载成功" : "下载失败");
    } catch(e) {
        alert(e.message);
    }
};
unitTest();

关键改进点:

  • 直接通过app.doScript()执行curl命令,省去中间脚本文件的生成与调用环节
  • 对路径和URL中的特殊字符(单引号)进行转义,避免shell命令解析报错
  • 调整等待间隔为10ms,优化资源占用

保留生成脚本文件的修复方案

如果需要保留生成脚本文件的逻辑,需修正引号拼接并通过osascript执行:

var getMacImage = function(exporturl, assetFolderPath, fileName) {
    try {
        var ff = new File(assetFolderPath + '/' + fileName);
        if (!ff.exists) {
            var escapedPath = ff.fsName.replace(/'/g, "'\\''");
            var escapedUrl = exporturl.replace(/'/g, "'\\''");
            // 正确拼接AppleScript代码
            var asCode = 'do shell script "curl -L -o \'' + escapedPath + '\' \'' + escapedUrl + '\'"';
            var scriptFile = new File(Folder.desktop + "/dl.applescript");
            scriptFile.open('w');
            scriptFile.write(asCode);
            scriptFile.close();
            // 通过osascript命令执行脚本文件
            var execCommand = 'do shell script "osascript \'' + scriptFile.fsName.replace(/'/g, "'\\''") + '\'"';
            app.doScript(execCommand, ScriptLanguage.APPLESCRIPT_LANGUAGE);
        }
        var c = 0;
        while(!ff.exists) {
            $.sleep(10);
            c++;
            if (c > 1000) {
                throw new Error("图片下载超时(超过10秒)");
            }
        }
        return ff;
    }
    catch(e) {
        throw new Error("从" + exporturl + "下载资源失败:" + e.message);
    }
};

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 10:42:40