如何在Apple Script中搜索后打开文件夹

问题描述 投票:0回答:1

标题有点令人困惑,但这就是我想要发生的事情:

  1. 打开查找器到文件夹“Grove”
  2. 弹出搜索命令(命令f)
  3. 在“Grove”中搜索包含搜索的所有文件
  4. 如果只有 1 个文件,则该文件将被打开

请帮忙

我对 Apple Script 有点陌生,所以我正在边学习边学习。到目前为止,我有 Apple 脚本打开查找器,然后打开文件夹“Grove”

这就是我目前拥有的

tell application "Finder" to open folder "Grove"
end
display dialog "Enter Project Number" default answer "" buttons {"Cancel", "Search"} default button 2
set {text returned:t, button returned:b} to result
if b is "Search" then
    set d to quoted form of (POSIX path of ((path to home folder as Unicode text) & "Grove:"))
end if
set found to paragraphs of (do shell script "mdfind -onlyin " & d & space & quoted form of t)
open result
end
applescript
1个回答
0
投票

尝试这样的事情:

set grover to quoted form of POSIX path of ((path to home folder as text) & "grove:")
--> "'/Users/username/grove/'"

display dialog "Enter Project Number" default answer "" buttons {"Cancel", "Search"} default button 2
set {text returned:t, button returned:b} to result

if b is "Search" then
    set found to (do shell script "mdfind -onlyin " & grover & space & quoted form of ("OV_" & t))
    -- shell command will look like this: 
    -- "mdfind -onlyin '/Users/username/grove/' 'OV_345'"
    -- assuming single hit, result should look like this:
    --> "/Users/username/grove/strategy and tactics.txt"
    
    if (count of paragraphs of found) is 1 then
        set foundOne to POSIX file found as «class furl»
        tell application "Finder" to open foundOne
    else
        display alert "More than one result"
    end if
end if

我将整个搜索位放在 if…then 块中,因为我假设如果用户取消,那么一切都应该停止。

我将

open
命令输入到 Finder,否则,脚本编辑器将尝试打开结果文件(取决于您运行脚本的方式)。 Finder 应该根据文件的扩展名知道哪个应用程序拥有要打开的文件。

© www.soinside.com 2019 - 2024. All rights reserved.