如何将 HEX 代码解码为 Apple Script 中的常规文本

How to decode HEX code to regular text in Apple Script

我在 Mac 上使用来自 Cultured Code 的待办事项列表应用程序 Things,当我复制一个 link 到剪贴板的任务,它最终在剪贴板中编码为 HEX 代码。当我将它粘贴到文本文件时没问题 – 然后它显示为解码文本。

但是,我需要在 AppleScript 中使用剪贴板内容,并且很难在那里将其解码为纯文本。

我尝试了多个子例程,但它们对我的情况不起作用。我在网上找到的大多数示例都处理简单的编码 URL。到目前为止,我的代码可以将例如“0348”正确解码为数字 1000,但我的脚本 cannot 解码编码的 Things link(那一长串数字在顶部)。

有人可以帮我吗?

这是我目前的情况:

-- The link to a task in THINGS, encoded: 7468696e67733a2f2f2f73686f773f69643d41463645303746462d394230462d343539332d423143332d313846303337434237363836
-- Above link to the task in THINGS, unencoded: things:///show?id=AF6E07FF-9B0F-4593-B1C3-18F037CB7686
-- Online converter: http://www.unit-conversion.info/texttools/hexadecimal/
-- Number 1000 encoded: 03E8

set theEncodedText to "03E8"
set theDecodedText to (do shell script "perl -e 'printf(hex(\"" & theEncodedText & "\"))'") as string

set theDisplayedText to theDecodedText
display dialog theDisplayedText

谢谢, 马丁

以下示例 AppleScript 代码概念证明 我将如何处理设置为 变量 的事物 URL link 保存到剪贴板为 «data url ...»,其中 ... 是十六进制数据。

这个脚本«data url ...»写入临时文件,读取临时文件,现在是«data url ...»的文本字符串,并设置它作为变量,然后删除临时文件。然后它将 Things URL link 显示为文本字符串或显示剪贴板不包含 'things:///show?id=' URL 的消息link.

示例 AppleScript 代码:

if ((clipboard info) as string) contains "URL" then

    set thingsURL to "/tmp/thingsURL.tmp"

    try
        set f to open for access thingsURL with write permission
        set eof f to 0
        write (the clipboard) to f
        close access f
    on error
        close access f
    end try

    set thingsURLtext to (read thingsURL)
    tell application "System Events" to delete file thingsURL

    display dialog thingsURLtext buttons {"OK"} default button 1

else

    display dialog "The clipboard did not contain a 'things:///show?id=' URL link." buttons {"OK"} default button 1

end if

注: 示例 AppleScript 代码 就是这样,并且没有使用任何其他 错误处理 然后显示的内容仅用于显示完成任务的多种方法之一。用户始终有责任 add/use 像 needed/wanted 一样适当地 错误处理