查找文件在 finder 问题中选择它 [英] Finding a file selecting it in finder issue

查看:16
本文介绍了查找文件在 finder 问题中选择它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个脚本来查找文件并选择它

I am having this script to find a file and select it

set filePath to ("filePath.ext" as POSIX file)
tell application "Finder"
    if (exists filePath) then
            select filePath
            activate
    else
        display alert "File " & filePath & " does not exist"
    end if
end tell

它在 Mac os x 10.6.x (LION) 上运行良好,但是当我尝试在 Mac os x 10.5.x (snow-leopard) 上运行此脚本时,它正在选择文件但花费了太多时间.任何建议如何使此代码在两个版本的 Mac 上都能正常工作.提前致谢:)

it is working perfect well on Mac os x 10.6.x (LION) but when I try to run this script on Mac os x 10.5.x (snow-leopard) it is selecting file but taking too much time. Any suggestion how can I make this code work fine on both versions of Mac. Thanks in advance :)



我正在从网络驱动器中选择文件,而旅馆系统具有 Windows 操作系统.所有系统都位于同一网络中.



I am selecting file from a network drive and hostel system is having a Windows Os. All systems are located in the same network.

推荐答案

reveal 命令可能对您有所帮助.它只是在查找器中找到一个文件,必要时打开一个新窗口,然后选择文件——所有这些只使用一行代码:

The reveal command may be of assistance to you. It simply locates a file in the finder, opens a new window if necessary, and then selects the file—all that using just one line of code:

tell application "Finder" to reveal path:to:some:file

当然,该文件必须实际存在才能正常工作.当以别名形式(即 Macintosh HD:Users:billybob:Desktop:howToHack.pdf)呈现特定文件/目录时,您就知道它存在.尝试将不存在的文件强制转换为别名将导致错误.如果您 100% 确定该文件存在并确切知道它的位置,那么恭喜!你少担心一件事.如果您的确定性水平低于 100%,请使用 try-catch 块.他们多次救了我的命.这样,如果您像我一样通过 Internet 分发您的应用程序,您的客户端就不会收到无法辨认的错误消息.

The file must actually exist for this to work, of course. You know a particular file/directory exists when it is presented in alias form (I.e. Macintosh HD:Users:billybob:Desktop:howToHack.pdf). Attempting to coerce a nonexistent file into an alias will result in an error. If you are 100% certain that the file exists and know exactly where it is, congratulations! You have one less thing to worry about. If your certainty level is anything less than 100%, use a try-catch block. They have saved my life on multiple occasions. That way, if you distribute your applications via the Internet like I do, your clients are not presented with undecipherable error messages.

一个例子如下所示:

set theFile to "/Users/billybob/Desktop/folder/subfolder/subfolder2/subfolder3/fineByMe.mp3"
try
    set theFile to (theFile) as alias
    tell application "Finder" to reveal theFile
on error
    display alert "The file " & quoted form of theFile & "does not exist."
    -- The variable 'theFile' couldn't be coerced into an alias.
    -- Therefore, 'theFile' still has a string value and it can be used in dialogs/alerts among other things.
end try

这比你写的更有效率还是更省时?老实说,我不是特别确定.但是,我在 Mac OS X 10.5.8 (Leopard)、Mac OS X 10.6.8 (Snow-Leopard) 和 Mac OS X 10.7.3 上编写了许多包含 reveal 命令的脚本(Lion),结果令人满意.

Is this more efficient or less time consuming than what you've written? I'm not particularly sure, to be honest. However, I have written many scripts that have included the reveal command on Mac OS X 10.5.8 (Leopard), Mac OS X 10.6.8 (Snow-Leopard), and Mac OS X 10.7.3 (Lion), and the results have been satisfying.

这篇关于查找文件在 finder 问题中选择它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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