【问题标题】:Copy files to several USB Sticks with AppleScript使用 AppleScript 将文件复制到多个 USB 记忆棒
【发布时间】:2013-01-10 10:04:30
【问题描述】:

我必须将文件复制到很多 U 盘。因此,我正在尝试给我写一个简短的小程序。我不熟悉applescript,所以如果有人能给我一些提示,那就太好了。

到目前为止我所拥有的:

10 个 USB 集线器

重命名棒的简短脚本。

现在我被困在将文件复制到每个连接的棒上:

property ignoredVolumes : {"Macintosh HD", "Time Machine Backups"}     

set myPath to ("Macintosh HD:Users:myusername:USB-Stick") as string
tell application "System Events"
set rootVolume to disk item (POSIX file "/Volumes" as text)
set allVolumes to name of every disk item of rootVolume
repeat with aVolume in allVolumes
    if aVolume is not in ignoredVolumes then
        set name of disk item (path of rootVolume & aVolume) to "Stickname"
    end if
end repeat
end tell

我现在需要做的是从 myPath 复制到每个连接的 USB-Stick。因为他们都有相同的名字,所以他们会在名字后面加上数字(Stickname,Stickname 1,Stickname 2,...)

所以我需要在我的循环中将复制命令添加到刚刚重命名的棒上。

希望有人能帮帮我。

【问题讨论】:

    标签: applescript


    【解决方案1】:

    使用 shell 脚本会更容易:

    do shell script "cp -R ~/USB-Stick/ /Volumes/Stickname*"
    

    【讨论】:

      【解决方案2】:

      你可以试试这样的:

      property ignoredVolumes : {"Macintosh HD", "Time Machine Backups", "iDisk", "net", "home"}
      set myPath to (path to home folder as text) & "USB-Stick:"
      tell application "System Events" to set theDisks to every disk
      repeat with aDisk in theDisks
          if aDisk's name is not in ignoredVolumes then
              set diskPath to path of aDisk
              tell application "Finder" to duplicate myPath to diskPath with replacing
          end if
      end repeat
      

      【讨论】:

        【解决方案3】:

        感谢您的提示。我现在用以下代码解决了它:

        property ignoredVolumes : {"Macintosh HD", "Time Machine Backups"}   -- add as needed
        property counter : 0
        tell application "System Events"
        set rootVolume to disk item (POSIX file "/Volumes" as text)
        set allVolumes to name of every disk item of rootVolume
        set StartTime to (get current date)
        repeat with aVolume in allVolumes
            if aVolume is not in ignoredVolumes then
                set newName to "XXX" & counter as string
                log newName & " in Progress... "
                set name of disk item (path of rootVolume & aVolume) to newName
                set counter to counter + 1
                set scr to ("cp ~/USB-Stick/* /Volumes/" & newName)
                do shell script scr
                set name of disk item (path of rootVolume & newName) to "XXX"
                do shell script "diskutil unmount /Volumes/XXX"
                log newName & " finished."
            end if
        end repeat
        set endTime to (get current date)
        set dur to (endTime - StartTime)
        display dialog "Process took " & dur & "seconds."
        end tell
        

        希望这能帮助其他有同样问题的人。

        只有一个外观问题:调用两个 shell 脚本会引发错误 -10004 但仍然有效。尝试告诉应用程序“Finder”或“终端”,但错误仍然存​​在并且已被其他错误添加,所以我坚持使用我的原始解决方案。

        亲切的问候

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-03-17
          • 2020-08-17
          • 1970-01-01
          • 1970-01-01
          • 2016-11-26
          • 2018-07-05
          • 1970-01-01
          相关资源
          最近更新 更多