我想编写一个AppleScript,它在与我当前打开的文件相同的目录中创建一个新文件(在这种情况下,在应用程序TexShop中)。
如果我可以获得路径,那么我可以编写如下脚本:
set thePath to get path to open window / tell TexShop to get path...?
set response to display dialog "Some text:" default answer ""
set toWrite to text returned of response
tell application "Finder" to set newFile to make new file at thePath
set openFile to open for access file (newFile as string) with write permission
write toWrite to openFile starting at eof
我最终只是在寻找一种方法来指定我希望文件根据变化的环境(即,当我在不同目录中的不同文件上工作时)的位置。
答案 0 :(得分:1)
下面的AppleScript代码应有助于实现在最前面的应用程序中获取最前面的文档的路径及其包含文件夹的路径的目标。
(* The Delay Command Gives You Time To Bring The Desired App To The Front
Mainly For Use While Testing This Code In Script Editor.app *)
delay 5 -- Can Be Removed If Not Needed
tell application (path to frontmost application as text) to ¬
set documentPath to (get path of document 1) as POSIX file as alias
tell application "Finder" to set containingFolder to container ¬
of documentPath as alias
答案 1 :(得分:0)
这是一种应同时涵盖可编写脚本和不可编写脚本的应用程序的技术,但需要注意的是,它显然无法返回开发人员无法通过可访问性挂钩提供的信息。但是,如果它是可编写脚本的,或者开发人员选择公开应用程序数据以实现可访问性,那么它将在AppleScriptable软件不断发展的范围之外提供很少的额外范围:
tell application id "com.apple.systemevents" to tell (the first process ¬
where it is frontmost) to tell (a reference to the front window) ¬
to if it exists then tell its attribute "AXDocument"'s value to ¬
if it is not in [missing value, "file:///Irrelevent"] then ¬
return my (POSIXPathOfFolder for it)
false
on POSIXPathOfFolder for (fileURL as text)
local fileURL
set fp to "/tmp/rw" as «class furl»
close access (open for access fp)
set eof of fp to 0
write the fileURL to fp
read fp as «class furl»
POSIX path of (result & "::" as text)
end POSIXPathOfFolder
我还尽量避免使用 Finder
,因为它速度慢,越野车和性情。底部的处理程序使用标准的附加功能以及内置功能来获取所检索文档路径的包含文件夹。
如果文档路径不可用,脚本将返回false
。