Finding a file selecting it in finder issue

半城伤御伤魂 提交于 2019-12-24 07:28:48

问题


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

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 :)

EDIT:

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


回答1:


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

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.

An example of this is demonstrated below:

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

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.




回答2:


You have errors in your code.

  1. You forgot a period after "exists" in the display alert line.
  2. You cannot display a posix file. It must be converted to string. Apple doesn't have this optimization.
  3. The exists command will always return false the way you've used it because you didn't provide a full file path. Although java and c++ allow abbreviated file paths, apple does not.

I cannot comment, so I was forced to put this as an answer instead.



来源:https://stackoverflow.com/questions/11222501/finding-a-file-selecting-it-in-finder-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!