Jør*_*gen 9 applescript finder
我试图制作一个AppleScript,它应该从Finder读取当前目录并在其上运行shell命令.如果我在Finder中导航到所需的文件夹并从AppleScript编辑器运行脚本它可以工作,但是当我保存脚本并将其拖到Finder工具栏时,currentDir被设置为脚本文件的文件夹(我的用户目录).这是我的脚本:
tell application "Finder"
set currentDir to POSIX path of ((container of (path to me)) as text)
end tell
tell application "Terminal"
do script "cd " & currentDir
do script "<operation goes here>"
end tell
Run Code Online (Sandbox Code Playgroud)
使用工具栏快捷方式时,如何激活目录?第二,有没有办法在后台运行shell命令,而无需打开(显示)终端窗口?
jac*_*300 13
这有两个解决方案:
1-如果当前文件夹是前Finder窗口的目标:
tell application "Finder" to set currentDir to (target of front Finder window) as text
do shell script "cd " & (quoted form of POSIX path of currentDir) & "; <operation goes here>"
Run Code Online (Sandbox Code Playgroud)
-
2 - 如果当前文件夹是所选文件夹,则相对于窗口目标(列表视图或封面流程)将有所不同,因为您可以在窗口中选择一个sufolder(窗口的目标不会更改) :
tell application "Finder"
set sel to item 1 of (get selection)
if class of sel is folder then
set currentDir to sel as text
else
set currentDir to (container of sel) as text
end if
end tell
do shell script "cd " & (quoted form of POSIX path of currentDir) & "; <operation goes here>"
Run Code Online (Sandbox Code Playgroud)
的insertion location是最前面的搜索窗口或桌面的标题栏上显示的文件夹中。
tell application "Finder" to POSIX path of (insertion location as alias)
Run Code Online (Sandbox Code Playgroud)
Windows 具有folder标题栏上显示的文件夹的属性:
tell application "Finder" to POSIX path of ((folder of window 1) as alias)
Run Code Online (Sandbox Code Playgroud)
在 10.7 和 10.8中有一个未解决的错误,它们(和selection属性)偶尔会引用它们的旧值。
这将使当前文件夹取决于列表视图中的选择:
tell application "Finder"
if current view of window 1 is in {list view, flow view} and selection is not {} then
set i to item 1 of (get selection)
if class of i is folder then
set p to i
else
set p to container of i
end if
else
set p to folder of window 1
end if
POSIX path of (p as alias)
end tell
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13851 次 |
| 最近记录: |