问题描述
我想拥有一个可以在其中运行的脚本中,该脚本基本上会使用在finder中选择的.rdp文件,并在其末尾添加一行文本。
例如
我下载了.rdp文件以在Microsoft远程访问中使用并加快我的工作流程,我想在启动文件之前在其末尾附加文本“ Use Multimon:i:1”,这样我就不会每次都必须打开首选项。
我对AppleScript不太熟悉,因此不胜感激如何实现这一目标的任何建议。
谢谢!
解决方法
这当前适用于扩展名为“ txt”的所选文件。测试它,如果它适合您,请将其编辑为所需的扩展名。
'if'语句是为了确保您不会意外地将文本附加到可能损坏该文件的二进制文件中。 “返回”表示您的文本将显示在自己的行上。如果您不想这样做,请删除“ return&”。有关“开放获取”的详细信息,请参阅Language Guide: Commands Reference。在同一页面上,您可以找到“关闭访问权限”。
tell application "Finder"
set tFile to selection as alias
if name extension of tFile is "txt" then
set corR to true
else
display alert "Are you sure you've selected the correct file?"
set corR to false
end if
end tell
if corR is true then
set ab to open for access tFile with write permission
write return & "Use Multimon:i:1" to ab starting at eof
close access ab
end if
,
据我了解,.RDP文件以纯文本格式保存。如果是这种情况,则在AppleScript中使用do shell script
命令,将文本附加到文件中非常简单。下面的AppleScript代码应该适合您。
将以下代码粘贴到新的Script Editor.app文档中。然后 在Finder中当前选择了.RDP文件的情况下,在Script Editor.app中运行代码,它将文本添加到文件中。
property addText : "Use Multimon:i:1"
tell application "Finder" to set selectedFile to POSIX path of ((get selection) as alias)
do shell script "echo " & quoted form of addText & " >> " & quoted form of selectedFile