V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
Lxcm
V2EX  ›  JavaScript

Illustrator 的 JSX 脚本输出文件名问题

  •  
  •   Lxcm · 2022-10-19 12:07:42 +08:00 · 1391 次点击
    这是一个创建于 778 天前的主题,其中的信息可能已经有所发展或是发生改变。
    //
    这是一个 Illustrator 的脚本,此脚本将实际日期或实际时间或文件名等插入到文档中的预定义位置。

    我现在想输出的文件名可以选择性输出内容。

    例如我的文件名:1.05.07.1908 能源参数页-V1(GLEM)GGI-UT0260E-0106 SS

    在 AI 文件里一个地方放:1.05.07.1908 ,一个地方放:能源参数页-V1(GLEM)GGI-UT0260E-0106 SS

    所以我想增加 2 个功能,
    只输出文件名前面数字部分:1.05.07.1908 (这串数字和.是固定这么长的,12 个字符?不是程序员不太懂)
    只输出文件名数字后面部分:能源参数页-V1(GLEM)GGI-UT0260E-0106 SS

    我在 var content = content.replace(/\{FILENAME\}/,docname)后增加.slice(0,12)
    可以单一的实现运行{FILENAME}得到:1.05.07.1908
    或者 var content = content.replace(/\{FILENAME\}/,docname)后增加.slice(12)
    运行{FILENAME}得到:能源参数页-V1(GLEM)GGI-UT0260E-0106 SS ,
    但这不是我想要的结果。

    我想能否定义一个{FILENAME2}得到:1.05.07.1908 ,
    定义一个{FILENAME3}得到:能源参数页-V1 (GLEM)GGI-UT0260E-0106 SS ,
    是不是缺少判断语句?

    因为我不是程序员,所以我也只会简单的改下一点参数之类的东西,所以想问下有人会修改吗?或者其他方法也行不一定跟着我思路走,可能我想法和你们不一样。下面是用到的脚本代码,可能运行在 Illustrator 上的不怎么规范,有人有空能否帮忙看下怎么改,先谢谢了。

    //

    // -------------------------------------------------------------------

    var language="en"; // "de" fuer Deutsch

    // -------------------------------------------------------------------

    var WR="WR-DateAndTime v0.9\n\n";

    var AIversion=version.slice(0,2);

    if (language == "de") {

    var format_preset = "{FILENAME}{FILEEXT} ({DATE} - {TIME})";

    var MSG_unsetmark = WR+"Dieses Objekt ist als aktuelles Datum/Uhrzeit markiert, soll die Markierung aufgehoben werden?";
    var MSG_setmark = WR+"Soll dieses Textobjekt als aktuelles Datum/Uhrzeit markiert werden?";
    var MSG_askformat = WR+"Soll das Textobjekt als Datum/Uhrzeit formatiert werden? Formate:\n{DATE}, {TIME}, {FILE}, {FILEPATH}, {FILENAME} und {FILEEXT}:"
    var MSG_editformat = WR+"Datums-/Uhrzeitformat bearbeiten (Leer = entfernen). Formate:\n{DATE}, {TIME}, {FILE}, {FILEPATH}, {FILENAME} und {FILEEXT}:"
    var MSG_notexto = WR+"Kein Textobjekt!";
    var MSG_selectedmany = "Zum Markieren als aktuelles Datum/Uhrzeit darf nur ein Textobjekt ausgew\xE4hlt sein und falls Sie die Daten aktualisieren wollen, darf kein Objekt ausgew\xE4hlt sein.";
    var MSG_nodocs = WR+"Kein Dokument ge\xF6ffnet."
    var Timeformat = 24;
    var TimeSep = ":";
    var AM = " am";
    var PM = " pm";
    var Dateformat = "dd.mm.yyyy";

    } else {

    var format_preset = "{FILENAME} ({DATE}, {TIME})";

    var MSG_unsetmark = WR+"This object is marked as actual date'n'time, do you want to remove the mark?";
    var MSG_setmark = WR+"Do you want to mark the selected textobject as actual date'n'time?";
    var MSG_askformat = WR+"Do you want to mark the textobject as actual date'n'time? Formats:\n{DATE}, {TIME}, {FILE}, {FILEPATH}, {FILENAME} and {FILEEXT}:"
    var MSG_editformat = WR+"Edit date'n'time (empty = remove). Formats:\n{DATE}, {TIME}, {FILE}, {FILEPATH}, {FILENAME} and {FILEEXT}:"
    var MSG_notexto = WR+"No textobject!";
    var MSG_selectedmany = "To mark as actual date'n'time, you have to select only one textobject. If you want to update the date'n'time-objects, there must be no object selected.";
    var MSG_nodocs = WR+"You have no open document."
    var Timeformat = 12;
    var TimeSep = ":";
    var AM = " am";
    var PM = " pm";
    var Dateformat = "mm/dd/yyyy";

    }

    var error=0;

    if (documents.length<1) {
    error++;
    alert(MSG_nodocs)
    }

    if (error < 1) {
    date_n_time();
    }

    function TodayDate()
    {
    var Today = new Date();
    var Day = Today.getDate();
    var Month = Today.getMonth() + 1;
    var Year = Today.getYear();
    var PreMon = ((Month < 10) ? "0" : "");
    var PreDay = ((Day < 10) ? "0" : "");
    if(Year < 999) Year += 1900;

    var theDate = Dateformat.replace(/dd/,PreDay+Day);
    theDate = theDate.replace(/mm/,PreMon+Month);
    theDate = theDate.replace(/d/,Day);
    theDate = theDate.replace(/m/,Month);
    theDate = theDate.replace(/yyyy/,Year);
    theDate = theDate.replace(/yy/,Year.toString().substr(2,2));

    return theDate;
    }

    function TodayTime()
    {
    var Today = new Date();
    var Hours = Today.getHours();
    var Minutes = Today.getMinutes();
    var Suffix = "";
    if (Timeformat == 12) {
    if (Hours >= 12 ) {
    Suffix = PM;
    } else {
    Suffix = AM;
    }
    if (Hours >= 13) {
    Hours = Hours - 12;
    }
    if (Hours < 1) {
    Hours = Hours + 12;
    }
    }
    var PreHour = ((Hours < 10) ? "0" : "");
    var PreMin = ((Minutes < 10) ? "0" : "");
    return PreHour+Hours+TimeSep+PreMin+Minutes+Suffix;
    }

    function DateUpdate(Name) {
    var docpath = activeDocument.path.fsName;
    var docname = activeDocument.name.replace(/(.*?)(?:\.([^.]+))?$/,'$1');
    var extension = activeDocument.name.replace(/(.*?)(?:(\.[^.]+))?$/,'$2');
    if (docpath.slice(2,3) == "\\") {
    docsep = "\\";
    } else {
    docsep = ":";
    }
    var content = Name.slice(11);
    var content = content.replace(/\{FILE\}/,docpath+docsep+docname);
    var content = content.replace(/\{FILEPATH\}/,docpath);
    var content = content.replace(/\{FILENAME\}/,docname);
    var content = content.replace(/\{FILEEXT\}/,extension);
    var content = content.replace(/\{DATE\}/,TodayDate());
    var content = content.replace(/\{TIME\}/,TodayTime());
    return content;
    }

    function date_n_time()
    {
    if (selection.length == 1) {
    if (selection[0].typename == "TextArtItem" || selection[0].typename == "TextFrame") {
    if (selection[0].name.slice(0,11) == "actualDate:") {
    dateformat = selection[0].name.slice(11);
    Check = false;
    if (AIversion == "10") {
    Check = confirm( MSG_unsetmark );
    } else {
    dateformat = prompt(MSG_editformat, dateformat);
    }
    if(dateformat != "" && Check) {
    selection[0].contents = selection[0].name.slice(11);
    selection[0].name="";
    selection[0].selected = false;
    }
    if(dateformat == "" && !Check) {
    selection[0].name="";
    selection[0].selected = false;
    }
    if(dateformat && dateformat !="" && !Check) {
    selection[0].name="actualDate:"+dateformat;
    selection[0].contents = DateUpdate(selection[0].name);
    }
    } else {
    dateformat = selection[0].contents;
    if(dateformat.search(/\{DATE\}/) == -1 && dateformat.search(/\{TIME\}/) == -1 && dateformat.search(/\{FILE[A-Z]*\}/) == -1) dateformat = format_preset;
    Check = false;
    if (AIversion == "10") {
    Check = confirm( MSG_setmark );
    } else {
    dateformat = prompt(MSG_askformat, dateformat);
    }
    if (dateformat || Check) {
    selection[0].name="actualDate:"+dateformat;
    selection[0].contents = DateUpdate(selection[0].name);
    selection[0].selected = false;
    }
    }
    } else {
    alert ( MSG_notexto );
    }
    } else if (selection.length > 1) {
    alert ( MSG_selectedmany );
    } else {
    if (AIversion == "10") {
    var textArtItems = activeDocument.textArtItems;
    for (var i = 0 ; i < textArtItems.length; i++)
    {
    if (textArtItems[i].name.slice(0,11) == "actualDate:") {
    textArtItems[i].selected = true;
    textArtItems[i].contents = DateUpdate(textArtItems[i].name);
    }
    }
    } else {
    var textFrames = activeDocument.textFrames;
    for (var i = 0 ; i < textFrames.length; i++)
    {
    if (textFrames[i].name.slice(0,11) == "actualDate:") {
    textFrames[i].selected = true;
    textFrames[i].contents = DateUpdate(textFrames[i].name);
    }
    }
    }
    }
    }
    3 条回复    2022-10-19 13:37:50 +08:00
    zcf0508
        1
    zcf0508  
       2022-10-19 12:38:25 +08:00
    没细看代码,不过这样应该可以

    var format_preset = "{FILENAME} ({DATE}, {TIME})";
    ->
    var format_preset = "{FILENAME1} {FILENAME2} ({DATE}, {TIME})";


    var content = content.replace(/\{FILENAME\}/,docname);
    - >
    var content = content.replace(/\{FILENAME1\}/,docname.slice(0, 12));
    var content = content.replace(/\{FILENAME2\}/,docname.slice(12));
    Lxcm
        2
    Lxcm  
    OP
       2022-10-19 13:17:39 +08:00
    @zcf0508 谢谢,增加这 2 行可以了,还以为要插入很多代码才行,一直没敢直接加上试试
    var content = content.replace(/\{FILENAME1\}/,docname.slice(0, 12));
    var content = content.replace(/\{FILENAME2\}/,docname.slice(12));
    Lxcm
        3
    Lxcm  
    OP
       2022-10-19 13:37:50 +08:00
    @zcf0508 还有个问题,能否在文件名:能源参数页-V1(GLEM)GGI-UT0260E-0106 SS 上用()()括号来判断换行呢?
    变成:
    能源参数页-V1
    (GLEM)
    GGI-UT0260E-0106 SS
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3647 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 04:21 · PVG 12:21 · LAX 20:21 · JFK 23:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.