I have a script which need to select a folder which is on the desktop:
tell application "Finder"
select folder "t" of desktop
end tell
Unfortunately, when it does this it does not select the file on the desktop, but rather expands the reference to folder “t” of folder “Desktop” of folder “deltatee” of folder “Users” of startup disk of application “Finder”, which has the side affect of selecting the folder not on the desktop, but rather opening the desktop in a new window and selecting that folder.
Does anyone have any suggestions for work arounds for this?
That’s annoying. The right folder’s selected - but via an icon in the wrong window!
I expect you know your own business, but do you really need to select the folder? You can open it, rename it, delete it, and do all sorts of stuff without having to select it at all. This would also prevent the extra window from opening.
I have a script (mapped to Cmd-Shift-N by iKey, formerly Youpi Key) which gives me a dialog asking for the name of the New Folder to create in the Finder. (I really liked this functionality from an extension in OS9.)
To properly emulate the New Folder command, I want to select the folder after it has been created.
Currently I just check that (insertion point as string) isnot equal to (desktop as string) before doing the selection.
Umm… no…
set theDialog to display dialog "Create a new folder named:" default answer "New Folder" buttons {"OK", "Cancel"} default button 1
if button returned of theDialog = "OK" then
tell application "Finder"
make new folder with properties {name: text returned of theDialog}
end tell
end if
This will ask the user for a folder name, then directly create the folder with the name. You don’t need to create a new folder, select it and then type the name, which is what it sounds like you’re trying to do.
I will stand by the original question: how do I select an object on the desktop?
Here is the code as it now stands (which doesn’t select the folder if it is made on the desktop). It also doesn’t return an eror if the user clicks on the cancel button.
property fName : "New Folder by Plaid Cow Solutions"
tell application "Finder"
activate
try
set theResult to display dialog "New Folder Name:" default answer fName buttons {"Cancel", "Create"} default button 2
on error
set theResult to {button returned:"Cancel"}
end try
if button returned of theResult is "Create" then
set loc to (insertion location)
set fName to (text returned of theResult)
make new folder at loc with properties {name:fName}
-- As long as we are not on the desktop, select the folder.
if (loc as string) is not (desktop as string) then
select folder fName of loc
end if
end if
end tell
The selecting of the newly created folder is to emulate the actual New Folder command (which automatically selects any new folder that has been created).