如何在 AppleScript 中使用 Base64 图像路径?
How can I use Base64 image paths in AppleScript?
我想出了一个 AppleScript,可以从 VPN Tracker 监控我的 VPN 连接。到目前为止,我的代码可以正常工作,这意味着它将正确的状态显示为文本。我创建了两个 PNG 文件,我将其转换为 Base64,并希望将它们用作状态输出,而不仅仅是文本。对图像进行 Base64 转换的原因是,这样我就可以与其他人共享脚本,而无需也共享实际图像并期望用户将它们放在他的 Mac.
上的某个位置
但是我不确定如何在 AppleScript 中解码这些 Base64 字符串,所以它最后显示的是实际图像。
这是我目前的代码(带有文本输出)
set conn_state to "" as string
if application "VPN Tracker 365" is running then
tell application "VPN Tracker 365"
try
if name of groups contains "group_name" then
set conn_state to state of connection of group ("group_name") as string
if conn_state = "On" then
return "VPN active"
else
return "VPN inactive"
end if
end if
on error
return "An error occured"
end try
end tell
end if
我确实在互联网上做了一些研究,但找不到任何可以帮助我解决这个问题的东西,或者我可能没有使用正确的搜索词。
如有任何帮助,我们将不胜感激。提前致谢
"The reason for the Base64 conversion of the images is, so I can share the script with others, without needing to share the actual images as well and expect the user to put them somewhere on his Mac."
考虑采用不同的方法。以下解决方案将使图像能够捆绑在您的 Applescript 中,并避免必须使用 Base64 encode/decode 它们:
通过 AppleScript 编辑器.
将您的 AppleScript 保存为 “应用程序” 格式
通过 “Finder”
找到您生成的应用程序
- 按住 ctrl 键的同时单击它。
- 通过上下文菜单选择“显示包内容”。
将您的 .png
张图片复制到 Contents/Resources
文件夹。
然后在你的代码中访问图片的路径如下:
# Get the pathname to where this script resides in the filesyetem.
set pathToMe to (path to me) as text
# Create the full pathname to the image
set pathToPng to pathToMe & "Contents:Resources:img.png" as alias
# Just a demo to illustrate that the image path can be accessed.
tell application "Preview" to open pathToPng
注意: 此示例代码假定您已将名为 img.png
的图像复制到 Contents/Resources
文件夹。它首先获取您的应用程序所在位置的路径,并将图像路径分配给 pathToPng
变量
编辑:
或者,正如评论中@user3439894所提到的,只需使用以下代码直接获取图像的路径:
# Create the full pathname to the image
set pathToPng to path to resource "img.png"
# Just a demo to illustrate that the image path can be accessed.
tell application "Preview" to open pathToPng
注:这里是利用path to resource
获取图片的路径,前面1-3的步骤还是要的
我想出了一个 AppleScript,可以从 VPN Tracker 监控我的 VPN 连接。到目前为止,我的代码可以正常工作,这意味着它将正确的状态显示为文本。我创建了两个 PNG 文件,我将其转换为 Base64,并希望将它们用作状态输出,而不仅仅是文本。对图像进行 Base64 转换的原因是,这样我就可以与其他人共享脚本,而无需也共享实际图像并期望用户将它们放在他的 Mac.
上的某个位置但是我不确定如何在 AppleScript 中解码这些 Base64 字符串,所以它最后显示的是实际图像。
这是我目前的代码(带有文本输出)
set conn_state to "" as string
if application "VPN Tracker 365" is running then
tell application "VPN Tracker 365"
try
if name of groups contains "group_name" then
set conn_state to state of connection of group ("group_name") as string
if conn_state = "On" then
return "VPN active"
else
return "VPN inactive"
end if
end if
on error
return "An error occured"
end try
end tell
end if
我确实在互联网上做了一些研究,但找不到任何可以帮助我解决这个问题的东西,或者我可能没有使用正确的搜索词。
如有任何帮助,我们将不胜感激。提前致谢
"The reason for the Base64 conversion of the images is, so I can share the script with others, without needing to share the actual images as well and expect the user to put them somewhere on his Mac."
考虑采用不同的方法。以下解决方案将使图像能够捆绑在您的 Applescript 中,并避免必须使用 Base64 encode/decode 它们:
通过 AppleScript 编辑器.
将您的 AppleScript 保存为 “应用程序” 格式通过 “Finder”
找到您生成的应用程序- 按住 ctrl 键的同时单击它。
- 通过上下文菜单选择“显示包内容”。
将您的
.png
张图片复制到Contents/Resources
文件夹。然后在你的代码中访问图片的路径如下:
# Get the pathname to where this script resides in the filesyetem. set pathToMe to (path to me) as text # Create the full pathname to the image set pathToPng to pathToMe & "Contents:Resources:img.png" as alias # Just a demo to illustrate that the image path can be accessed. tell application "Preview" to open pathToPng
注意: 此示例代码假定您已将名为
img.png
的图像复制到Contents/Resources
文件夹。它首先获取您的应用程序所在位置的路径,并将图像路径分配给pathToPng
变量
编辑:
或者,正如评论中@user3439894所提到的,只需使用以下代码直接获取图像的路径:
# Create the full pathname to the image
set pathToPng to path to resource "img.png"
# Just a demo to illustrate that the image path can be accessed.
tell application "Preview" to open pathToPng
注:这里是利用path to resource
获取图片的路径,前面1-3的步骤还是要的