我们使用 AppleScript 在 Finder 中显示来自我们应用程序的文件。如果用户安装了替代程序(例如 PathFinder)来替代 Finder,我们如何才能找到要告诉 AppleScript 命令的应用程序?
答案1
您可以尝试使用这个:
try
tell application "Path Finder" to reveal "/Users/danielbeck/Downloads"
on error
tell application "Finder" to reveal folder "Downloads" of home
end try
但这假设使用 Path Finder 的用户更喜欢它的显示功能。
或者,
do shell script "open 'file:///Users/danielbeck/Downloads'"
当用户配置了 Path Finder 来处理file://
URL 时,这将在 Path Finder 中打开文件夹。但仅适用于文件夹。
您可以使用以下命令获取进程列表:
tell application "System Events"
processes
end tell
查找名为 Finder 的进程。如果未找到,则用户没有正在运行的 Finder。或者查找名为 Path Finder 的进程,如果找到,则使用它。等等。
答案2
虽然@Daniel Beck 在上面提供了一个很好的答案,但这里有一个处理程序,我用它来在 Finder 中显示当前在 Path Finder 中选择的项目。我经常需要在 Finder 窗口中执行某些操作,因此我添加了一个暂停,直到 Finder 窗口名称与 Path Finder 窗口名称相同。
为了将来参考,我将这个代码片段保存在我的 GistHub 中: revealPFItemInFinder.applescript
```applescript
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on revealPFItemInFinder()
--–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
(* VER: 2.0 2018-03-15
PURPOSE: Reveal Item in Finder that is Selected in Path Finder
RETURNS: alias of item selected in both Finder and Path Finder
AUTHOR: JMichaelTX
--–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
*)
--- GET THE ITEM SELECTED IN PATH FINDER ---
tell application "Path Finder"
set fileList to (get selection)
if ((fileList is missing value) or ((count of fileList) ≠ 1)) then error ("You must select only ONE file in Path Finder.")
set pfWinName to name of window 1
set itemPath to POSIX path of item 1 of fileList
end tell
set itemAlias to alias POSIX file itemPath
--- REVEAL SAME ITEM IN FINDER ---
tell application "Finder"
activate -- to make sure reveal will be in frontmost window
reveal itemAlias
--- Now Wait for New Finder Window with Same Name as Path Finder ---
set finWinName to name of window 1
set maxWaitTime to 2.0
set delayTime to 0.1
set waitTime to 0
repeat while finWinName ≠ pfWinName
delay delayTime
set finWinName to name of window 1
set waitTime to waitTime + delayTime
if (waitTime > maxWaitTime) then error "Max wait time of " & maxWaitTime & " exceeded waiting for Finder Window of " & pfWinName
end repeat
end tell
return itemAlias
end revealPFItemInFinder
```