"Clean up" desktop with Applescript

时间:2015-12-10 01:25:37

标签: macos applescript

I feel it should be a simple task, but I could not find a solution yet. Basically, I want to clean up the items of the desktop using Applescript, the same way I would by right-clicking the desktop and clicking "Clean up".

Unfortunately, something like the following does not work:

tell application "Finder" to clean up desktop

Any ideas?

2 个答案:

答案 0 :(得分:0)

这似乎对我有用:

tell application "System Events"
    tell application "Finder"
        activate desktop
    end tell
    -- this delay is to ensure that the second block of code doesnt run too fast, or it will not work, if this doesn't work, try increasing this delay
    delay 0.1
    tell process "Finder"
    -- this clicks the the clean up button from the menu bar at the top, the "activate desktop" at the beginning was to make sure that the desktop's menu bar was the front menu bar
        click menu item "Clean Up" of menu "View" of menu bar item "View" of front menu bar
    end tell
end tell

编辑:新改进的脚本

tell application "System Events"
-- hides everything, which makes the menu bar become the desktop's menu bar
    set visible of every process to false
    set visible of process "Finder" to false
end tell
delay 0.1
tell application "System Events"
    tell process "Finder"
        click menu item "Clean Up" of menu "View" of menu bar item "View" of front menu bar
    end tell
end tell

答案 1 :(得分:0)

这就是我使用的:

set theFiles to {}
set folderExtensions to {}
set _extensions to {}
tell application "Finder" to set theFiles to every file in folder (desktop as string)
    tell application "Finder"
 if not (folder ((desktop as string) & "Mydesktop") exists) then    
 make folder at (desktop as string) with properties {name:"Mydesktop"}      
end if
end tell
set i to 0
    repeat (number of items in theFiles) times
 set i to i + 1
 set _extensions to _extensions & (name extension of (info for file ((item i of theFiles) as string)))
     end repeat
set i to 0
    repeat (number of items in _extensions) times
 set i to i + 1
 if folderExtensions does not contain (item i of _extensions) then    
set folderExtensions to folderExtensions & (item i of _extensions)
 end if
end repeat
set i to 0
    repeat (number of items in folderExtensions) times
 set i to i + 1    
tell application "Finder"
 if not (folder ((desktop as string) & "Mydesktop:" & (item i of folderExtensions)) exists) then
      make folder at ((desktop as string) & "Mydesktop:") with properties {name:(item i of folderExtensions)}
          end if    
end tell
end repeat
set i to 0 ------we are moving the files now
    set _key to ""
set x to 1
set letters to {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
    repeat
 set x to x + 1
 if x > 100 then return
 set _key to ""
 repeat 20 times    
 set _key to _key & (item (random number from 1 to 26) of letters)     
end repeat     
tell application "Finder"    
if not (folder ((desktop as string) & "Mydesktop:" & _key) exists) then     
make folder at folder ((desktop as string) & "Mydesktop:") with properties {name:_key}    
exit repeat
end if
end tell
end repeat
repeat (number of items in theFiles) times
 set i to i + 1
 tell application "Finder"
 if not (file ((desktop as string) & "Mydesktop:" & (item i of _extensions) & ":" & (name of (info for file ((item i of theFiles) as string)))) exists)
then
 move file ((item i of theFiles) as string) to folder ((desktop as string) & "Mydesktop:" & (item i of _extensions) & ":")
 else
      set x to 1
 repeat    
set x to x + 1
 if x > 100 then exit repeat
 if not (file ((desktop as string) & "Mydesktop:" & (item i of _extensions) & ":" & (name of (info for file ((item i of theFiles) as string))) & " " & x) exists) then
 set someItem to (item i of theFiles)         
tell application "System Events" to tell disk item (someItem as text) to set {theName, theExtension} to {name, name extension}
 if theExtension is not "" then set theName to text 1 thru -((count theExtension) + 2) of theName -- the name part    

set old to ((desktop as string) & "Mydesktop:" & _key & ":" & (name of (info for file ((item i of theFiles) as string))))    
move file ((item i of theFiles) as string) to folder ((desktop as string) & "Mydesktop:" & _key & ":")    
set the name of file old to theName & " " & x & "." & theExtension
 move file ((desktop as string) & "Mydesktop:" & _key & ":" & theName & " " & x & "." & theExtension) to folder ((desktop as string) & "Mydesktop:" & (item i of _extensions) & ":")    
exit repeat
 end if    
end repeat
 end if
 end tell    
end repeat
set item1 to (the quoted form of POSIX path of ((((path to desktop) as string) & "Mydesktop:" & _key) as alias))
set deleteit to "rm -rf " & item1 & ""
try
 do shell script deleteit
on error
 do shell script deleteit with administrator privileges
end try    

它创建一个文件夹(如果它还不存在)并对我的所有文件进行排序。
我已将其导出为应用程序并将其设置为每次登录时打开in!这样我开始使用一个干净的桌面。