如何获取应用程序打开的窗口的文件的路径? [英] How do I get the path to the file of an open window of an application?

查看:161
本文介绍了如何获取应用程序打开的窗口的文件的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个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

我最终只是在寻找一种方法来指定我希望文件根据变化的环境(即,当我在不同目录中的不同文件上工作时)的去向.

解决方案

下面的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

I want to write an AppleScript which creates a new file in the same directory as the file I currently have open (in this case, in the application TexShop).

If I can get the path, then I can write a script like:

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

I am ultimately just looking for a way to specify where I want the file to go depending on changing circumstances, that is, when I am working on different files in different directories.

解决方案

This following AppleScript code should help towards the goal of getting the path to the front most document in the front most application and the path to its containing folder.

(* 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

这篇关于如何获取应用程序打开的窗口的文件的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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