Posted By

ianthekirkland on 05/08/12


Tagged


Versions (?)

F-GetFolderPathToFinderItemOrWindow


 / Published in: AppleScript
 

  1. (* This always gets a path to a folder *)
  2. tell application "Finder"
  3. try
  4. -- Try to get the current item(s) selected in Finder first
  5. set mySel to selection
  6. if (count of mySel) < 1 then
  7. -- If there's no selection try to get the path of the window instead
  8. -- Otherwise just return the desktop since the user could have used the context menu from there
  9. if (count of windows) > 0 then
  10. set myFolder to target of first window
  11. else
  12. set myFolder to desktop
  13. end if
  14. else
  15. set myFolder to first item of mySel
  16. end if
  17.  
  18. if (count of mySel) > 1 then
  19. display dialog "Multiple items selected, only getting first item of " & quoted form of (displayed name of myFolder as text) with icon
  20. end if
  21.  
  22. -- If the selection is a file or an alias to a file we get the containing folder instead
  23. if class of myFolder is document file or (class of myFolder is alias file and file type of myFolder is missing value) then
  24. set myFolder to container of myFolder
  25. end if
  26.  
  27. -- Do whatever you want with the folder here...
  28. on error errMsg
  29. beep
  30. display dialog errMsg
  31. return
  32. end try
  33. end tell
  34.  

Report this snippet  

You need to login to post a comment.