使用AppleScript将文件复制到多个USB Sticks

时间:2012-09-06 06:23:10

标签: applescript

我必须将文件复制到很多USB-Sticks中。所以我试着写一个简短的苹果。我不熟悉Applecript,所以如果有人能给我一些提示,那将会很棒。

到目前为止我所拥有的:

10位USB-Hub

重命名小册子的简短脚本。

现在我不停地将文件复制到每个连接的棒上:

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,...)

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

希望有人能帮助我。

3 个答案:

答案 0 :(得分:1)

使用shell脚本会更容易:

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

答案 1 :(得分:1)

您可以尝试这样的事情:

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

答案 2 :(得分:0)

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

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”或“终端”,但错误仍然存​​在并且已被其他错误添加,因此我坚持使用我的原始解决方案。

亲切的问候