如何使用applescript将桌面元素(图标,文件,文件夹)保存在同一个地方

时间:2016-11-12 07:05:26

标签: applescript desktop

使用脚本我们将创建另一个脚本,其中将是桌面所有元素的存储位置,创建的脚本将被编译并可用于将之前受保护的所有元素放回原位。

/adesktopsave/deskico.txt它是用于编译的临时文件。 /adesktopsave/savedicoposition.scpt这是编译为与applescrit一起使用的保存脚本

此处使用的所有名称仅用于示例。这些名字没有特别的属性。

在使用此脚本之前,只需要计划创建一个文件夹。这里是: / adesktopsave

其他,在“尝试之后”行尾(\ n) “ 也“结束尝试 “ 和& “}  “)

尊重是非常重要的,以便文本可用。

    tell application "Finder" to set theList to {name, desktop position} of items of desktop 
try 
do shell script "rm -f /adesktopsave/deskico.txt"

do shell script "echo tell application " & quoted form of (quote & "Finder" & quote) & return & " >>/adesktopsave/deskico.txt" 

end try 

set n to (count (first item of theList))


repeat with i from 1 to n       

set inp to do shell script "echo " & quoted form of (item i of first item of theList)       

set xy to (item i of second item of theList)    

set AppleScript's text item delimiters to ","   

set xyz to do shell script "echo " & xy     
set wxyz to ("{" & xyz & "}
 ")         

set ligne to "try 
" & "set desktop position of item " & quoted form of (quote & inp & quote) & " of desktop to " & quoted form of (wxyz) & "end try 
"       
set ligne to do shell script "echo " & ligne & " >>/adesktopsave/deskico.txt"    

end repeat 

do shell script "echo " & "end tell" & return & " >>/adesktopsave/deskico.txt"

    display dialog "Do you want to save your icons in their current location?" buttons {"Cancel", "Save"} default button 2 with title "Save  the positions of icons"

    if (button returned of result) is "Cancel" then     
set n to do shell script "echo " & n 
else    
do shell script "osacompile -o " & "/adesktopsave/savedicoposition.scpt" & " /adesktopsave/deskico.txt" 
end if 

return n

1 个答案:

答案 0 :(得分:0)

我们可以将脚本简化为最简单的表达式。存在错误的风险可以是。

set ligne to ""
do shell script "mkdir -p  /adesktopsave"
tell application "Finder" to set {names, positions} to {name, desktop position} of items of the desktop
set ligne to "tell application \"Finder\"
"
set n to (count names)
set AppleScript's text item delimiters to ","
repeat with i from 1 to n
set ligne to ligne & ("try
" & "set desktop position of item " & (quote & item i of names & quote) & "  to  {" & item i of positions & "}
end try
")
end repeat
set ligne to ligne & ("end tell" & return)
display dialog "Do you want to save your icons in their current location?" buttons {"Cancel", "Save"} default button 2 with title "Save the positions of icons"
  if (button returned of result) is "Cancel" then 
    set n to do shell script "echo " & n
else
  do shell script "osacompile -o " & "/adesktopsave/savedicoposition.scpt  -e  "  & quoted form of ligne
end if
set AppleScript's text item delimiters to ""
tell application "Finder" to open POSIX file "/adesktopsave/savedicoposition.scpt"
return n