2009-05-23 10 views
9

Tôi cần tạo một AppleSript để sao chép các tập tin được chỉ định từ một thư mục vào một thư mục mới được tạo.Sao chép tập tin vào một thư mục mới

Những tập tin này cần phải được quy định trong trình soạn thảo AppleScript nên cái gì như:

start 

fileToBeMoved = "Desktop/Test Folder 1/test.doc" 
newfoldername = "Test Folder 2" 

make newfolder and name it 'newfoldername' 
copy 'fileToBeMoved' to 'newfolder' 

end 

Trả lời

4

Thách thức đối với AppleScript là file di chuyển được thực hiện sử dụng bí danh.

Thực tế hơn, có thể dễ dàng hơn khi tạo tập lệnh shell thay vì có thể chạy từ AppleScript bằng cách sử dụng do shell script nếu bạn đang sử dụng Automator hoặc tương tự.

#!/bin/sh 

fileToBeMoved = "~/Desktop/Test Folder 1/test.doc" 
newFolderName = "Test Folder 2" 
mkdir $newFolderName 
cp $fileToBeMoved $newFolderName 
+0

của nó tốt để sử dụng phương pháp này so với giá cũ AppleScript gây đôi khi bạn có thể có vấn đề cho phép và bằng cách này sẽ giúp bạn. –

+0

Nhược điểm của việc này là: Người dùng sẽ không thấy thanh tiến trình nếu bản sao mất nhiều thời gian hơn. Người dùng cũng không thể hủy vì cùng một lý do. Hỗ trợ các hoạt động này với một lệnh shell sẽ khó khăn hơn rất nhiều so với việc cho phép Finder thực hiện nó. Nhược điểm của Applescripting Finder, mặc dù: Nếu người dùng chọn sử dụng một ứng dụng "Finder" thay thế, hoạt động Applescript có thể thất bại (không chắc chắn về điều này, có thể phụ thuộc vào cách thay thế được viết). –

+1

Một điểm cộng khác cho việc sử dụng Trình tìm kiếm: Nếu mục đích tồn tại, người dùng sẽ được yêu cầu thay thế hay không. –

9

chung:

tell application "Finder" 
    make new folder at alias "Macintosh HD:Users:user:Desktop:" with properties {name:"Test Folder 2"} 
    copy file "Macintosh HD:Users:user:Desktop:Test Folder 1:test.doc" to folder "Macintosh HD:Users:user:Desktop:Test Folder 2" 
end tell 

Bạn có thể thêm tên biến đại diện cho các file POSIX và đường dẫn.

Rõ ràng ký tự dấu hai chấm (:) là ký tự dành riêng cho thư mục và tên tệp.

set desktopFolder to "Macintosh HD/Users/user/Desktop/" 
set desktopFdrPosix to quoted form of POSIX path of desktopFolder 
set newFolderName to "Test Folder 2" 
set destinationFdrPosix to quoted form of desktopFdrPosix & POSIX file newFolderName 
set sourceFilename to "Test Folder 1/test.doc" 
set sourceFnPosix to quoted form of desktopFdrPosix & POSIX file sourceFilename 

tell application "Finder" 
    make new folder at alias desktopFdrPosix with properties {name:newFolderName} 
    copy file sourceFnPosix to folder destinationFdrPosix 
end tell  

Bạn cũng có thể muốn kiểm tra lỗi nếu thư mục đích đã tồn tại.

3
set home_path to path to home folder as string 
set work_folder to alias (home_path & "AutomationAppleScript:ScreenShots:") 
tell application "Finder" 
    duplicate every file of work_folder to folder "Archive" of home 
end tell 
1

này hoạt động sao chép một khối lượng mạng gắn:

mount volume "afp://compname.local/mountpoint" 
    tell application "Finder" 
     duplicate file "MyTextFile.txt" of folder "Documents" of home to disk "mountpoint" 
     eject "mountpoint" 
    end tell 
0

Nếu đó là một đồng bộ bạn đang tìm kiếm bạn có thể chạy các script vỏ sau trong AppleScript của bạn:

rsync -a /Users/username/folderToBeCopiedFrom /Volumes/folderToBeCopiedTo/

0
tell application "Finder" 
    make new folder at desktop with properties {name:"folder"} 
    duplicate POSIX file "/usr/share/doc/bash/bash.html" to result 
end tell 

POSIX file ((system attribute "HOME") & "/Documents" as text) 
tell application "Finder" to result 

tell application "Finder" to duplicate (get selection) to desktop replacing yes 

-- these are documented in the dictionary of System Events 
path to home folder 
POSIX path of (path to documents folder) 
path to library folder from user domain 
path to desktop folder as text 

-- getting files as alias list is faster 
tell application "Finder" 
    files of entire contents of (path to preferences folder) as alias list 
end tell 
0

Tôi đã sử dụng giải pháp tập lệnh shell Chealions. Đừng quên làm cho tệp tập lệnh của bạn có thể thực thi được với: sudo chmod u+x scriptFilename

Đồng thời xóa khoảng trắng giữa các bài tập biến số =. Sẽ không làm việc với các không gian, ví dụ: newFolderName="Test Folder 2"

0

Cách đơn giản để làm điều đó là như dưới đây nói

set home_path to path to home folder as string 
set work_folder to alias (home_path & "AutomationAppleScript:ScreenShots:") 
tell application "Finder" 
    duplicate every file of work_folder to folder "Archive" of home 
end tell