Monday, September 21, 2020

#1 2019-10-29 10:03:31 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1297

Positioning Finder item's icons on the desktop

I wrote the following script having come across this article: Can I use AppleScript to reset items on my desktop?
As I have on my desktop shortcut to Scripts folder of user domain (that is, alias), I can do this:

Applescript:


tell application "Finder"
   set myAlias to alias file "Scripts alias" of desktop
   set desktop position of myAlias to {1000, 400}
end tell

or, this:

Applescript:


tell application "Finder"
   select alias file "Scripts alias"
   set desktop position of item 1 of (get selection) to {100, 600}
   close front Finder window
end tell

Last edited by KniazidisR (2019-10-29 10:21:09 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#2 2019-10-29 10:24:46 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 615

Re: Positioning Finder item's icons on the desktop

Thanks KniazidisR--that's very useful. With a few additional lines of code, I can position icons on my desktop just as I want. This script sorts icons by categories, which consist of broken aliases then folders then files. Within each of these categories, icons are sorted by name. The user is notified of broken aliases, although this can be disabled.

Applescript:


set iconStartPosition to {75, 75} -- x and y coordinates of first icon
set iconPadding to 110 -- make negative for bottom-to-top icon ordering
set columnPadding to 110 -- make negative for right-to-left column ordering
set columnIconCount to 6 -- maximum number of icons in a column
set notifyBrokenAlias to true -- notify if an alias is broken

set {desktopFolders, desktopFiles} to {{}, {}}

tell application "Finder"
   
   set desktopIcons to sort (every item of (path to desktop)) by name
   
   repeat with anItem in desktopIcons
       if (class of anItem) = alias file then
           try
               if class of (original item of anItem) = folder then
                   set end of desktopFolders to anItem
               else
                   set end of desktopFiles to anItem
               end if
           on error
               if notifyBrokenAlias then my brokenAliasDialog(anItem)
               set beginning of desktopFolders to anItem
           end try
       else
           if (class of anItem) = folder then
               set end of desktopFolders to anItem
           else
               set end of desktopFiles to anItem
           end if
       end if
   end repeat
   
   set iconCount to 0
   set iconPosition to iconStartPosition
   repeat with anItem in (desktopFolders & desktopFiles)
       set desktop position of anItem to iconPosition
       set iconCount to iconCount + 1
       if ((iconCount mod columnIconCount) = 0) then
           set iconPosition to {((item 1 of iconPosition) + columnPadding), (item 2 of iconStartPosition)}
       else
           set iconPosition to {(item 1 of iconPosition), ((item 2 of iconPosition) + iconPadding)}
       end if
   end repeat
   
end tell

on brokenAliasDialog(anItem)
   set dialogText to "The folder or file referenced by desktop icon " & quote & (name of anItem) & quote & " may not exist."
   display dialog dialogText buttons {"OK"} default button 1 with title "Desktop Reset" with icon caution
end brokenAliasDialog

Last edited by peavine (2019-11-29 10:19:42 am)


2018 Mac mini - macOS Catalina

Offline

 

#3 2020-09-14 09:42:35 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 615

Re: Positioning Finder item's icons on the desktop

After almost a year's use, I decided that my script in post 2 did too much and so I simplified it significantly. The operation of the revised script requires no explanation except to note:

- For the script to operate, the Finder sort-by view preference has to be set to none.

- If the user wants the icons to align with the right side of the screen, the columnSpace value has to be negative.

- Sort order options include date created, date modified, kind, name, and size.

Applescript:

on main()
   
   set firstIcon to {1850, 150} -- x and y coordinates of first icon
   set iconSpace to 120 -- vertical space between icons
   set columnSpace to -100 -- make negative for right-to-left column ordering
   set columnIconCount to 7 -- maximum number of icons in a column
   
   set iconCount to 0
   set nextIcon to firstIcon
   
   tell application "Finder"
       set iconList to sort every item of (path to desktop) by name
       -- set iconList to reverse of iconList -- reverse sort order
       repeat with anIcon in iconList
           set desktop position of anIcon to nextIcon
           set iconCount to iconCount + 1
           if (iconCount mod columnIconCount) = 0 then
               set nextIcon to {(item 1 of nextIcon) + columnSpace, item 2 of firstIcon}
           else
               set nextIcon to {item 1 of nextIcon, (item 2 of nextIcon) + iconSpace}
           end if
       end repeat
   end tell
   
end main

main()

Last edited by peavine (2020-09-18 08:58:14 am)


2018 Mac mini - macOS Catalina

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)