How to tell if a Desktop file or folder is selected in the finder?

Is there a way for an application to tell if desktop files/folders are selected in the Finder?

Several apps can do that so it should be possible in Xojo, too. At first glance, I wasn’t able to find anything for NSFileManagerMBS. AppleScript should work, too. See Posix path of Finder selection query... - AppleScript | Mac OS X - MacScripter .

1 Like

A cheap way to do it via a Shell:

var finderSelection as new Shell
finderSelection.Execute( _
    "/usr/bin/osascript", _
    "-e 'if running of application ""Finder"" then tell application ""Finder"" to selection'" _
if finderSelection.Result.Trim <> "" then
  // There is a selection 
  MessageBox finderSelection.Result.Trim
end if

Thanks Beatrix and Kem! I was able to combine both of your methods to get a list of selected file/folders, and then removed the ones that weren’t on the Desktop.

In case anyone else runs into the problem I found with Kem’s script. The string that is returned is comma delimited, so file names with a comma in them are something you have to watch for if you’re using Split.String to separate file paths/names.