Thursday, December 5, 2019

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

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

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
macOS Mojave -- version 10.14.4
Safari -- version 12.1
Firefox -- version 70.0

Offline

 

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

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

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 2019-11-21 08:12:49 am

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

Re: Positioning Finder item's icons on the desktop

I place aliases rather than files and folders on the desktop and wanted some flexibility in how these aliases are created and named. So, I wrote the script included below, which works as follows:

1) If a file or folder is selected in the Finder, then the script creates an alias for this item on the desktop.

2) If a Finder window is open but a file or folder is not selected, then an alias is created on the desktop for the target of the front Finder window.

3) The name of the created alias is the name of the file or folder, although this is easily changed to whatever the user prefers.

4) If the alias name already exists, the script does nothing.

I've found the simplest method to use this script is to save it as an application and to command-drag the script file to the Finder toolbar to create an icon. With later versions of macOS, security preferences for the script will need to be set.

Applescript:


tell application "Finder"
   
   try
       set finderSelection to selection as alias list
       if finderSelection = {} then set end of finderSelection to (target of front Finder window) as alias
   on error
       error number -128
   end try
   
   repeat with anItem in finderSelection
       try
           set itemName to (name of anItem)
           make new alias file at desktop to anItem with properties {name:itemName}
       end try
   end repeat
   
end tell

Last edited by peavine (2019-11-22 07:53:03 am)


2018 Mac mini - macOS Catalina

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)