需要在ExtendScript中为Windows系统生成UUID [英] need to generate UUID for windows system in ExtendScript

查看:120
本文介绍了需要在ExtendScript中为Windows系统生成UUID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用以下代码为的链接生成 DocumentID InstanceID .indd 文件。

I have used the below code to generate DocumentID and InstanceID for the links of .indd files. This works fine on MacOS.

谁能在Windows系统上建议类似的 UUID 生成代码。 Windows系统上是否有可用的此类库?

Can anyone suggest similar UUID generation code on Windows system. Is there any such library available on windows system?

function generateUUID() {
  var cmd = 'do shell script "uuidgen | tr -d " & quoted form of "-"';
  return app.doScript(cmd, ScriptLanguage.applescriptLanguage);
}

var genDocID = 'xmp.did:' + generateUUID();


推荐答案

TLDR; 以下是通过的ExtendScript API ...

TLDR; Below are a couple of different ways to generate a UUID on a Windows system via adobe-indesign's ExtendScript API...


  • 解决方案A > 掏空 通过VBScript到Windows Powershell的命令。但是,此解决方案确实需要安装Windows PowerShell,并具有运行VBScript的权限。

  • Solution A "shells out" a command to Windows Powershell via VBScript. However, this solution does require Windows PowerShell to be installed, and permission to run VBScript's.

解决方案B 利用InDesign本身来生成UUID。为此,它创建了一个临时 .indd 文档并提取了 DocumentID 。该解决方案可以成功地跨平台(在MacOS和Windows上)运行,不需要其他依赖项,并且比 Solution A 更具性能,因为它不必像

Solution B utilizes InDesign itself to generate the UUID. It achieves this by creating a temporary .indd document and extracting it's DocumentID. This solution runs successfully cross-platform (both MacOS and Windows), requires no additional dependencies, and is slightly more performant than Solution A because it doesn't have to hop between different coding languages unlike Solution A which does.

Windows上的默认外壳,即 cmd.exe 不提供内置实用程序来生成通用唯一标识符(UUID)。这与Bash实用程序 uuidgen 相对,后者在MacOS和其他 *上可用nix 平台。

The default shell on Windows, namely cmd.exe, does not provide a built-in utility to generate Universally Unique Identifier's (UUID). This is in contrast to the Bash utility uuidgen which is available on MacOS and other *nix platforms.

但是,可以通过Windows

However, it is possible to generate a UUID via Windows powershell by executing the following command:

[guid]::Newguid()

以下Adobe ExtendScript( win-generate-uuid.jsx )演示了如何利用上述PowerShell命令生成UUID。

The following Adobe ExtendScript (win-generate-uuid.jsx) demonstrates how a UUID can be generated by utilizing the aforementioned PowerShell command.

注意:此解决方案要求:


  • 要安装的Windows PowerShell。

  • 允许Windows运行vbscript '

  • Windows PowerShell to be installed.
  • Windows is permissible to run vbscript's.

win-generate-uuid.jsx

/**
 * Generates a unique identifier (UUID/GUID) by running a VBScript that
 * executes a PowerShell command.
 * @returns {String} - The generated unique identifier.
 */
function generateUUID() {
  var pwshCmd = '$guid = [guid]::Newguid(); $guid = [string]$guid';
  var vbScript = [
    'CreateObject("WScript.Shell").Run "powershell.exe -command ""' +
        pwshCmd + '"" | clip", 0, True',
    'guid = CreateObject("htmlfile").ParentWindow.ClipboardData.GetData("text")',
    'returnValue = Replace(guid, "-","")'
  ].join('\r');

  return app.doScript(vbScript, ScriptLanguage.visualBasic);
}

var genDocID = 'xmp.did:' + generateUUID();
$.writeln(genDocID);

说明:


  1. win-generate-uuid.jsx 中的 generateUUID 函数利用InDesign的 doScript() 运行VBScript的方法。

  1. The generateUUID function in win-generate-uuid.jsx utilizes InDesign's doScript() Method to run a VBScript.

执行的VBScript本质上使用运行上述PowerShell命令(尽管版本稍有修改)。 href = https://ss64.com/vb/run.html rel = nofollow noreferrer> Run() 命令。

The VBScript that is executed essentially runs the aforementioned PowerShell command, (albeit a slightly modified version), using the Run() command.

注意:必须使用VBScript对PowerShell命令进行 emshell操作,因为Windows上运行的InDesign仅允许使用VBScript或JavaScript可以通过 doScript 方法执行。

Note: It's necessary to utilize VBScript to "shell out" the PowerShell command because InDesign running on Windows allows only VBScript or JavaScript to be executed via it's doScript method.

PowerShell命令的结果(即生成的UUID) )通过管道( | )到剪贴板。

The result of the PowerShell command (i.e. the generated UUID) is piped (|) to the Clipboard.

子序列


  • 从剪贴板中检索UUID。

  • 所有连字符(<$ c $最终生成的UUID中的c>-)被删除,最后返回将其返回到 .jsx 脚本。

  • The UUID is retrieved from the Clipboard.
  • All hypens (-) in the generated UUID are removed, before finally return'ing it to the .jsx script.

有关VBScript为什么 Run()<使用/ code>(结合到剪贴板的管道),而不是VBScript的 Exec() 请参考此答案。原因摘要如下:

For further explanation of the reasons why VBScript's Run() is utilized, (combined with piping to the Clipboard), instead of VBScript's Exec() refer to this answer. A summary of the reasons are;


  • Run()不显示PowerShell窗口。

  • Exec()确实简要显示了PowerShell窗口。

  • Run() doesn't show the PowerShell window.
  • Exec() does briefly show the PowerShell window.






解决方案B:



用于UUID生成的跨平台解决方案(即在MacOS和Windows上成功运行的解决方案)是利用InDesign本身。这在下面的 generate-uuid.jsx 中得到了证明。

generate-uuid.jsx >

#target indesign

$.level=0

/**
 * Loads the AdobeXMPScript library.
 * @returns {Boolean} True if the library loaded successfully, otherwise false.
 */
function loadXMPLibrary() {
  if (!ExternalObject.AdobeXMPScript) {
    try {
      ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
    } catch (e) {
      alert('Failed loading AdobeXMPScript library\n' + e.message, 'Error', true);
      return false;
    }
  }
  return true;
}

/**
 * Generates a unique identifier (UUID/GUID) cross-platforms (macOS/Windows).
 * @returns {String} - The generated unique identifier.
 */
function generateUUID() {
  var tmp_FilePath = File(Folder.temp + '/__temp__.indd');

  // 1. Create temporary .indd and save it to disk.
  var newDoc = app.documents.add(false);
  newDoc.save(tmp_FilePath);
  newDoc.close();

  // 2. Extract the DocumentID from temporay .indd
  var xmpFile = new XMPFile(tmp_FilePath.fsName, XMPConst.FILE_INDESIGN, XMPConst.OPEN_FOR_READ);
  var xmp = xmpFile.getXMP();
  var documentID = xmp.getProperty(XMPConst.NS_XMP_MM, 'DocumentID', XMPConst.STRING);

  // 3. Delete temporary .indd
  tmp_FilePath.remove();

  // 4. Return the DocumentID without the default `xmp.did:` prefix.
  return String(documentID).replace(/xmp\.did:/g, '');
}


if (loadXMPLibrary()) {
   var genDocID = 'xmp.did:' + generateUUID();
   $.writeln(genDocID);
}

说明:

generate-uuid.jsx 脚本(上面)包含名为 generateUUID 的函数本质上执行以下操作:

The generate-uuid.jsx script (above) contains a function named generateUUID that essentially performs the following:


  1. 创建一个新的InDesign文档( .indd ),并将其保存到操作系统的临时文件夹中,然后将其关闭。该任务在后台执行,因此用户将不知道实际的文档已创建。

  1. Creates a new InDesign document (.indd) and saves it to the OS's temporary folder, then closes it. This task is performed in the background so the user will not know that the actual document has been created.

注意操作系统的默认临时目录为通过 Folder.temp 。为了进一步了解temporay文件夹在每个操作系统中的位置,您可以在脚本中临时添加以下代码行,并将其路径名记录到您的ExtenScript控制台中:

Note The OS's default temporary directory is determined via Folder.temp. To further understand where the temporay folder resides per OS you can temporarily add the following line of code to your script, and it will log it's pathname to your ExtenScript console:

$.writeln(Folder.temp);


  • 接下来,我们提取 DocumentID 从新创建的临时 .indd 文件使用 XMP脚本API -您应该从我对先前问题的回答中熟悉它; 此处此处在这里

  • Next we extract the DocumentID from the newly created temporary .indd file utilizing features of the XMP scripting API - which you should be familiar with from my answers to your previous questions; here, here, and here.

    最后,从提取的 documentID 中删除​​默认的 xmp.did:前缀。

    Finally, the default xmp.did: prefix from the extracted documentID is removed.

    注意:默认的 xmp.did:前缀已从<$的正文中删除。然后恢复c $ c> generateUUID 函数-似乎有点奇怪!我这样做是有意的,这样 generateUUID 仍然可以重用。例如;您可能希望使用它还生成一个 InstanceID 等,在这种情况下,您可能希望在UUID前面加上 xmp.iid:

    Note: The default xmp.did: prefix is removed in the body of the generateUUID function then reinstated - which may seem a bit strange! I've done this intentionally so that the generateUUID remains more reusable. For example; you may want to use it to also generate an InstanceID etc, in which case you'll probably want to prefix the UUID with xmp.iid:.

    这篇关于需要在ExtendScript中为Windows系统生成UUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆