Sunday, October 17, 2021

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

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2079

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 14.1
Ram: 4 GB

Offline

 

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

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

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 - Script Debugger 8

Offline

 

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

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

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 - Script Debugger 8

Offline

 

#4 2020-10-07 12:40:47 pm

kempederson
Member
Registered: 2020-09-02
Posts: 19

Re: Positioning Finder item's icons on the desktop

Applescript:




global fl, f

tell application "Finder"
   set fl to files of folder POSIX file "/Users/alias/" as alias list
end tell

tell application "Finder"
   repeat with f in fl
       tell application "Finder"
           open f
           activate
       end tell
   end repeat
end tell

repeat with j from 1 to 2
   tell application "Finder"
       set theCount to count of Finder windows
       repeat with i from 1 to theCount
           set theWindow to Finder window i
           tell theWindow
               set properties to {current view:list view, statusbar visible:true, sidebar width:0, toolbar visible:false, text size:10}
               tell its list view options
                   set properties to {text size:10}
               end tell
           end tell
       end repeat
   end tell
end repeat

tell application "Finder"
   repeat with f in fl
       tell application "Finder"
           open f
           activate
       end tell
   end repeat
end tell

--set window position

tell application "Finder" to set the bounds of the window "alias name1" to {0, 23, 316, 258}
tell application "Finder" to set the bounds of the window "alias name2" to {0, 259, 316, 505}



to find your {0, 259, 316, 505} of a selected window

tell application "Finder"
   set theCount to count of Finder windows
   repeat with i from 1 to 1
       set theBounds to {0, 0, 0, 0}
       set theWindow to ""
       set theWindow to Finder window i --to front window
       tell theWindow
           set theName to name of theWindow
           set theBounds to the bounds of the theWindow
       end tell
   end repeat
end tell
{theName, theBounds}

Offline

 

#5 2021-10-08 02:53:12 pm

kerflooey
Member
Registered: 2011-07-07
Posts: 208

Re: Positioning Finder item's icons on the desktop

-- I thought I would add this "positioning icons on the desktop" script here. The script aligns icons on the desktop according to the selected icons' leftmost or topmost icon position. (Topmost align code is commented out.) It works for me as-is, but you may want to customize it or improve it. I save a left-align version and a top-align version as apps with an "L" or "T" Finder icon, and put them in my Dock.
-- Tested in Mac OS X 10.15.7 Catalina

Applescript:


tell application "Finder"
   
   set theItems to the selection
   
   set boundsList to {}
   repeat with anItem in theItems
       
       set theseBounds to (desktop position of anItem)
       set boundsList to boundsList & {theseBounds}
       
   end repeat
   
   --return boundsList --> {{2092, 1238}, {2128, 1352}, {2116, 1467}} -- test 3 icons
   
   
   -------------------------------------------Get minLeft
   
   set minLeft to item 1 of (item 1 of boundsList)
   
   repeat with thisPosition in boundsList
       
       set thisLeft to item 1 of thisPosition
       
       if thisLeft is less than minLeft then
           set minLeft to thisLeft
       end if
       
   end repeat
   
   -------------------------------------------Get minTop
   
   set minTop to item 2 of (item 1 of boundsList)
   
   repeat with thisPosition in boundsList
       
       set thisTop to item 2 of thisPosition
       
       if thisTop is less than minTop then
           set minTop to thisTop
       end if
       
   end repeat

   
   -------------------------------------------SET minLeft
   
   repeat with itemPosition in theItems
       
       set dtPos to desktop position of itemPosition
       
       set (item 1 of (dtPos)) to minLeft
       
       set desktop position of itemPosition to dtPos
       
   end repeat
   
   
   (*
   -------------------------------------------SET minTop
   
   repeat with itemPosition in theItems
   
       set dtPos to desktop position of itemPosition
       
       set (item 2 of (dtPos)) to minTop

       set desktop position of itemPosition to dtPos
       
   end repeat
   *)

   
end tell

Browser: Safari 605.1.15
Operating System: macOS 10.14

Offline

 

#6 2021-10-11 07:41:43 pm

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

Re: Positioning Finder item's icons on the desktop

kerflooey. I tested your script which works well, but I did notice one possible issue. If no desktop icons are selected, the script throws the following error:

Can’t get item 1 of {}. (-1728)



Thanks for the script.

Last edited by peavine (2021-10-12 07:50:25 am)


2018 Mac mini - macOS Catalina - Script Debugger 8

Offline

 

#7 2021-10-12 10:23:19 am

kerflooey
Member
Registered: 2011-07-07
Posts: 208

Re: Positioning Finder item's icons on the desktop

Thanks, peavine. Yeah, I left out the error checking. Again! -k

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)