Applescript:需要帮助解决 - 无法获取所述文件夹的每个文件?

时间:2017-09-29 14:19:30

标签: applescript

在此表单上的某些用户的帮助下,我可以在下面创建此脚本,您可以选择一个文件夹,然后选择要将文件夹中的哪些文件夹同步到OneDrive备份文件夹中。

基本上是从现场网络存储迁移到OneDrive for Business,并希望为我们的用户创建尽可能简单的脚本。

我遇到的问题是我不希望用户能够选择原始文件夹,我想预先设置变量。

在我使用的脚本中:

set theFolder to (choose folder with prompt "Please Choose The Root of Your H Drive Or The Folder That Looks Like:    " & userName & "$")

如果我使用:

set theFolder to "/Volumes/MYERSMI5$/"

我收到“无法获取所述文件夹的所有文件”错误消息。

如何提前为此脚本设置theFolder,而不是要求用户选择该文件夹?

set OuserName to do shell script "whoami"
set userName to do shell script "echo " & OuserName & " | tr a-z A-Z"

tell application "Finder"
   if not (disk userName exists) then
       mount volume "SMB Server/" & userName & "$"
   end if

 delay 2

   set theDialogText to "
       - Mac H-Drive Migration Tool -

This Application Will Migrate a Copy of Your H Drive Data
to your OneDrive for Buisness Folder Locally on Your Mac

Migration Backup Location: 
/Users/" & OuserName & "/OneDrive Folder/H-Drive Migration Backup

** Important **    
In the Next Window Please Choose 
The Root Folder of Your H Drive
The Drive Label Should Look Like:    " & userName & "$"

   display dialog theDialogText

   set theFolder to (choose folder with prompt "Please Choose The Root of Your H Drive Or The Folder That Looks Like:    " & userName & "$")  

   do shell script "mkdir -p ~/'OneDrive Folder'/'H-Drive Migration Backup'"

   set HDriveBackupFolder to ((path to home folder as text) & "OneDrive Folder:H-Drive Migration Backup")

   set AppName to "OneDrive.app"

   tell application "Finder" to set Answer_ to exists application file ((path to applications folder as string) & AppName)
   if Answer_ is false then

       beep
       beep
       beep
       beep
       beep

   end if

   delay 1.5

   tell application "Finder"
       activate
       set theFolderNames to name of folders of theFolder
       set theChosenNames to (choose from list theFolderNames with prompt "Choose Which Folders to Backup, Please Hold Down The ⌘ Key To Choose Multiple Folders " with multiple selections allowed)
       if (theChosenNames is false) then return

       set HDriveBackupFolder to ((path to home folder as text) & "OneDrive Folder:H-Drive Migration Backup")
   end tell

   repeat with thisName in theChosenNames

       tell application "Terminal"

           do script ("rsync -avpz --delete " & (quoted form of POSIX path of ((theFolder as text) & thisName)) & space & (quoted form of POSIX path of HDriveBackupFolder)

       end tell
   end repeat
end tell

1 个答案:

答案 0 :(得分:0)

你问:

  

“如何提前为此脚本设置theFolder,而不是要求用户选择文件夹?”

此外,就在此之前,你说:

  

如果我使用:

   set theFolder to "/Volumes/MYERSMI5$/"
     

我收到“无法获取所述文件夹的所有文件”错误消息。

首先,让地址设置theFolder 变量。它应该如下:

set theFolder to POSIX file "/Volumes/MYERSMI5$/"

我认为错误是在代码中的以下点抛出的:

   tell application "Finder"
       activate
       set theFolderNames to name of folders of theFolder

set theFolderNames to name of folders of theFolder更改为:

set theFolderNames to name of folders of container theFolder

为了对此进行测试,我在MYERSMI5$处安装了一个名为/Volumes/,并在/Volumes/MYERSMI5$/内创建了一些文件夹。然后运行以下代码

set theFolder to POSIX file "/Volumes/MYERSMI5$/"
tell application "Finder"
    activate
    set theFolderNames to name of folders of container theFolder
    set theChosenNames to (choose from list theFolderNames with prompt "Choose Which Folders to Backup, Please Hold Down The ⌘ Key To Choose Multiple Folders " with multiple selections allowed)
    if (theChosenNames is false) then return
end tell

它生成了一个列表框,其中包含我在该位置创建的文件夹的名称,可供选择。

我没有尝试运行您在问题中包含的整个代码,所以如果您遇到其他问题,则需要在完成我提到的更改后进行跟进回答。有很好的理由可以解释涉及调试代码的问题是否符合How to create a Minimal, Complete,and Verifiable example

您可能还想查看Variables and Properties中的AppleScript Language Guide部分。