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

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

问题描述

我想编写一个 AppleScript,在与我当前打开的文件相同的目录中创建一个新文件(在本例中,在应用程序 TexShop 中).

如果我能得到路径,那么我可以写一个脚本:

set thePath 以获取打开窗口的路径/告诉 TexShop 获取路径...?设置响应显示对话框一些文本:"默认答案"将 toWrite 设置为响应返回的文本告诉应用程序Finder"设置 newFile 以在 thePath 处创建新文件将 openFile 设置为打开以访问具有写入权限的文件(newFile 作为字符串)从 eof 开始写入 toWrite 到 openFile

我最终只是在寻找一种方法来根据不断变化的情况(即,当我处理不同目录中的不同文件时)指定我希望文件的位置.

解决方案

以下 AppleScript 代码应该有助于实现获取最前端应用程序中最前端文档的路径及其包含文件夹的路径的目标.

>

(* Delay 命令让您有时间将所需的应用程序带到前台主要用于在 Script Editor.app 中测试此代码时使用 *)延迟 5 -- 如果不需要可以删除将应用程序(最前面的应用程序的路径作为文本)告诉 ¬将 documentPath 设置为(获取文档 1 的路径)作为 POSIX 文件作为别名告诉应用程序Finder"将包含文件夹设置为容器 ¬的 documentPath 作为别名

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天全站免登陆