Get the position of icons on the desktop - cocoa

How can I get the position of an icon on the desktop in OS X? Or, how can I get a list of all icons on the desktop and their positions?

Try this AppleScript code
tell application "Finder"
get desktop position of every item of desktop
end tell

The solution for a folder is to use a catalog iterator to get the Finder info for every item in the folder. The Finder info is a FileInfo or FolderInfo structure that contains a QuickDraw Point (integral co-ordinates, origin top-left, positive y down) specifying where the icon will appear.
I don't know how you would do this for the Desktop, though. There is a Desktop folder in the Home folder, but examining its contents will probably get you the positions that the Finder will use to display that folder in a window. It also will probably not include anything “on the Desktop” that isn't in the Desktop folder, such as mounted volumes.

following apple script will get those positions (use either scripting bridge or NSAppleScript)
tell application "Finder"
tell every item of desktop
get position
get name
end tell
end tell

Another solution (Very similar to the one of #Vaskravchuk)
tell application "Finder"
repeat with anItem in desktop
get {desktop position, name} of anItem
end repeat
end tell

Related

How can I create a .app file to open DMG files with, to run AppleScript with that file as input?

What I ultimately want to do is have a .app that I can open DMG files with. When the DMG file is opened the app, it opens the DMG in a new Finder tab and then shows the Toolbar in Finder after half a second. AppleScript for the later would look like this:
delay 0.5
tell application "Finder"
tell the front window to set toolbar visible to true
end tell
I tried looking up how to open a DMG file in a new Finder Tab with AppleScript but didn't find a solution. If I end up not finding one, then I'll see if I can do so with a Terminal command and then do it in AS with do shell script. For the file input, I tested this code based off what I found here but it doesn't work, it just creates a copy of the DMG:
on run {input, parameters}
if input is {} then
set inputFile1 to ¬
quoted form of POSIX path of ¬
(choose file with prompt "Please select a file to process:")
else
set inputFile1 to quoted form of ¬
(POSIX path of first item of input)
end if
display dialog inputFile1
end run
I did a bunch of googling as well for how to do so but I found nothing. What I can do to end up with an AppleScript that does everything I described?
I would open the dmg with hdiutil which seems to open the image into a standard window:
set runDMG to quoted form of POSIX path of (choose file)
do shell script "/usr/bin/hdiutil attach " & runDMG
Basically, you get the posix path of your dmg file and feed it to hdiutil attach. The disk image will open and —for me at least— consistently opens in a default finder window (i.e. same size as other windows, list view, sidebar visible, toolbar visible).
I should add that typically when I double-click on a dmg, it opens in a 'drag to applications folder' window which is icon view and shows only the app and the applications folder.
Separately, this code seems to work with bringing the disk image's folder to the front window.
tell application "Finder"
parent of startup disk
--> computer container of application "Finder"
open computer container
set target of window 1 to (last item of window 1 as alias)
--> disk "Key Codes" of application "Finder"
end tell
There are (at least on my Sierra mac), three items in the 'computer' folder: 'Network', 'Remote Disc', the startup volume. When you open a dmg, the virtual appears here as a 'removable'. I've found that when you get a list of folders in the computer container, the removable is the last item in the list. I don't know if that ever changes, and I haven't played around with multiple disk images simultaneously but it provides a starting point for specifying the desired window if for some reason, it doesn't open automatically.

Can't set Desktop Background on Mac with AppleScript

I am trying to set the Desktop Wallpaper of my Mac(Running latest version of Catalina). But I keep getting this error message when trying to run my apple script.
error "System Events got an error: Can’t set file \"Library:Desktop Pictures:Ink Cloud.jpg:\" of current desktop to file \"Library:Desktop Pictures:Ink Cloud.jpg:\" of current desktop." number -10006 from file "Library:Desktop Pictures:Ink Cloud.jpg:" of current desktop
here is my code
tell application "System Events"
tell current desktop
set picture rotation to 0
set picture to file "Library:Desktop Pictures:Ink Cloud.jpg:"
end tell
end tell
I've been able to change all the other properties of the Desktop except the actual photo. I also tried using / for the file path. I have tried different file paths. But still not luck. Any help or advice would be greatly appreciated.
Running the code
tell application "System Events"
set currentPicturePath to picture of current desktop
end tell
reveals that the path is supposed to be a (slash separated) POSIX path
tell application "System Events"
tell current desktop
set picture rotation to 0
set picture to "/Library/Desktop Pictures/Ink Cloud.jpg"
end tell
end tell
Your (colon separated) HFS path contains two mistakes:
An HFS path (unlike the POSIX path) starts always with a disk name.
Only references to folders and packages have a trailing colon.

Applescript (10.6.8). how to start app from user input

I am developing an automated test for several Mac OSX apps with applescript. The app should do the following.
1.) Display dialog shows up, where the user can type in 1 or more app names he want to be tested for example (1.test.app, 2.autotest.app,....)
2.)depending on how many apps names he has typed in, the apps should start and close consecutively to check if they are working.
So for example if the user type in apptest1.app, apptest2.app, apptest3.app -> the first app starting should be apptest1.app and then close it, the next app should be apptest2.app start and close and so on.
thank you very much.
lg,
San
This should do the trick
tell application "Finder"
set thePath to path to applications folder
set theApps to get name of every file of thePath
set apps_to_test to choose from list (theApps) with multiple selections allowed
end tell
repeat with the_app in apps_to_test
tell application the_app
activate
quit saving no
end tell
end repeat

Creating an automator service to create a new document in the current directory

so I'm trying to create a service that will be located in the contextual menu of the Finder and that would allow to create a new document in the current directory.
I've been doing that using Automator:
Sorry everything's in French ^^
Anyway here's the AppleScript that I'm using to retrieve the current working directory:
on run {input, parameters}
tell application "Finder"
set pwdAlias to insertion location as alias
if not (exists folder pwdAlias) then
set pwdAlias to (container of pwdAlias) as alias
end if
end tell
set pwd to POSIX path of pwdAlias
return pwd
end run
Then I'm setting this value to a variable, then creating a new text document using the variable as the path for the document and finally I'm using the command Reveal in Finder to show the created document.
Everything's is working fine except that the script seems to always be late!
What I mean is that when I open a new Finder window and select my service, it is systematically creating the document on the previous window as shown below:
But then if I try a second time, the document is being created properly at the expected location:
And this is very systematic it happens every time!!
Sorry if I'm not very clear, it is not so easy to explain!
Well otherwise, I'm running Mountain Lion and here's the Automator project attached: create_new_document
To add the service just unzip and put the file under ~/Library/Services/
Hope to get some answers but I fear that this is just an Automator bug!
Try this
Depending on what you want to be clicking.
Set the Services selected to: 'folders'
or files or folders. in 'Finder.app'
Get first Finder Window path Action
You can download the Get first Finder Window path Action from my blog post here
The download is at the bottom of the post.
The Action gets the posix path of the frontmost finder window.
Since you are clicking on a folder in a window. that window will be the one returned.
Set Value of Variable
Get Specified Text
The next action 'New Text File' needs some input. If it does not get any, no file will be created. You can leave the text field blank. Just having the action in place works.
New Text File
Drag the Variable 'path' or what ever you named it on to the Where: drop down menu.
you can click the double blue lines at the bottom of the Automator window to toggle the workflow Variable List
Save your service. And try it.
(It may take a short while to show up in the contextual Menu.)
It's an open bug in 10.7 and 10.8
Use this Workaround
on run {input, parameters}
activate application "System Events"
activate application "Finder"
tell application "Finder"
set pwdAlias to insertion location as alias
set pwdAlias to (container of pwdAlias) as alias
end tell
return POSIX path of pwdAlias
end run

Specify a Finder item by its path

is it possible to specify a Finder item by its path?
e.g. I want to refresh the icon of a single item. So far, to do this, I call update on every item of the front window:
tell application "Finder" to update every item in front window
But this only works if the folder currently has the item. Is it possible to specify an item with its path, so that the update will work even if the item is not visible in Finder?
tell application "Finder" to update item_x
where item_x is the item i want to update?
sure just give it an alias but then your hard coding it in there
tell application "Finder" to update "path:to:your:file.ext"
of course there are many more approaches you can take for this it just depend on how you want to get the item(s) you could
have a script in your menu that asks for an item using choose file
have a script in your menu that asks for a folder using choose folder
maybe some other options I'm sure.. can you provide more information what the process is ?

Resources