2016-10-31 47 views
0

我有一個簡單的代碼來刪除文件和製作文件夾。但是,如何讓它在任何筆記本電腦上運行,因爲每個人的用戶名都是不同的。我知道這與「〜」有關,但我不確定在哪裏。 下面的代碼工作正常,但我怎麼確保它可以在任何Mac上運行:使蘋果腳本代碼在任何筆記本電腦上工作(不需要用戶名)

try 
    tell application "Finder" 
    delete ((POSIX file "/Users/myname/Desktop/thefile.txt")) 
    make new folder at (POSIX file ("/Users/myname/Desktop")) with properties {name:"thefolder"} 
    end tell 
on error 
    display dialog ("That didnt work") buttons {":("} 
end try 

回答

0

語法獨立的用戶名的是由於當前用戶的桌面文件夾更簡單是文件夾的Finder

try 
    tell application "Finder" 
     delete file "thefile.txt" 
     make new folder at desktop with properties {name:"thefolder"} 
    end tell 
on error 
    display dialog ("That didnt work") buttons {":("} 
end try 
+0

嗨感謝您的回答。我給了桌面只是一個例子。我實際上想在庫中的文件夾中執行此操作。那我該怎麼做? –

+0

用戶庫的路徑是從用戶域到庫文件夾的路徑。 – vadian

+0

如何在庫中的一個文件夾稱爲消息。如何達到? –

0
You can also  

set username to "" 

     tell application "System Events" 

      set username to full name of current user 

     end tell 

     try 
      tell application "Finder" 
      delete ((POSIX file "/Users/username/Desktop/thefile.txt")) 
      make new folder at (POSIX file ("/Users/username/Desktop")) with properties {name:"thefolder"} 
      end tell 
     on error 
      display dialog ("That didnt work") buttons {":("} 
     end try 
相關問題