Computer >> Máy Tính >  >> Hệ thống >> MAC

5 Sử dụng Applescript của Mac để cải thiện năng suất

5 Sử dụng Applescript của Mac để cải thiện năng suất

Applescript là ngôn ngữ kịch bản hơi khó hiểu của Apple, nhưng nó là một công cụ mạnh mẽ để các chương trình mới bắt đầu sử dụng. Với một số Applescripts thông minh có thể xử lý các tác vụ khó chịu, chúng tôi có thể tăng năng suất và tự động hóa công việc của bạn.

Applescript là gì?

5 Sử dụng Applescript của Mac để cải thiện năng suất

Applescript giao diện với hầu hết các ứng dụng Mac như Finder, iTunes, QuickTime và Mail. Nếu bạn đã quen thuộc với Automator, thì Applescript giống như một phiên bản dành cho người dùng thành thạo của ứng dụng đó.

1. Chuyển đổi tệp ẩn

Lưu nó dưới dạng ứng dụng và bạn sẽ có một nút chuyển đổi có thể nhấp để hiển thị các tệp ẩn trong Finder.

set newHiddenState to "YES"
try
    set oldHiddenState to do shell script "defaults read com.apple.finder AppleShowAllFiles"
    if oldHiddenState is in {"1", "YES"} then
        set newHiddenState to "NO"
    end if
end try
do shell script "defaults write com.apple.finder AppleShowAllFiles " & newHiddenState
do shell script "killAll Finder"

2. Đổi tên hàng loạt tệp

Tập lệnh này sẽ nhắc người dùng nhập tên tệp và sau đó tự động đổi tên các tệp đã chọn bằng chuỗi văn bản đó cộng với chỉ mục tăng dần. Nó thậm chí còn thêm một cách hữu ích các số 0 ở đầu cho các tệp từ một đến mười.

-- This code comes from https://gist.github.com/oliveratgithub/
-- Open in AppleScript Editor and save as Application
-- ------------------------------------------------------------
--this is required to break the filename into pieces (separate name and extension)
set text item delimiters to "."
tell application "Finder"
    set all_files to every item of (choose file with prompt "Choose the Files you'd like to rename:" with multiple selections allowed) as list
    display dialog "New file name:" default answer ""
    set new_name to text returned of result
    --now we start looping through all selected files. 'index' is our counter that we initially set to 1 and then count up with every file.
    --the 'index' number is of course required for the sequential renaming of our files!
    repeat with index from 1 to the count of all_files
        --using our index, we select the appropriate file from our list
        set this_file to item index of all_files
        set file_name_count to text items of (get name of this_file)
        --if the index number is lower than 10, we will add a preceding "0" for a proper filename sorting later
        if index is less than 10 then
            set index_prefix to "0"
        else
            set index_prefix to "" 
        end if
        --
        --lets check if the current file from our list (based on index-number) has even any file-extension
        if number of file_name_count is 1 then
            --file_name-count = 1 means, we extracted only 1 text-string from the full file name. So there is no file-extension present.
            set file_extension to ""
        else
            --yup, we are currently processing a file that has a file-extension
            --we have to re-add the original file-extension after changing the name of the file!
            set file_extension to "." & item -1 of file_name_count
        end if
        --let's rename our file, add the sequential number from 'index' and add the file-extension to it
        set the name of this_file to new_name & index_prefix & index & file_extension as string
    end repeat
    --congratulations for successfully accomplishing the batch renaming task :)
    display alert "All done! Renamed " & index & " files with '" & new_name & "' for you. Have a great day! :)"
end tell

3. Chia tỷ lệ hình ảnh theo phần trăm

Tập lệnh này sẽ chia tỷ lệ hình ảnh thành 50% so với kích thước ban đầu của chúng.

-- Prompt for an image
set theImageFile to choose file of type "public.image" with prompt "Please select an image:"
 
-- Locate an output folder
set theOutputFolder to (path to desktop folder as string)
 
-- Launch Image Events
tell application "Image Events"
    launch
 
    -- Open the image
    set theImage to open theImageFile
    tell theImage
 
        -- Determine a save name for the image
        set theName to name
        set theSaveName to "smlr-" & theName
 
        -- Scale the image by 50%
        scale by factor 0.5
 
        -- Save the image to the output folder, using the save name
        save as file type in (theOutputFolder & theSaveName)
 
        -- Close the image
        close
    end tell
end tell

4. Chia tỷ lệ hình ảnh thành chiều rộng pixel

Điều này sử dụng phần lớn phần bắt đầu của các tập lệnh trước đó nhưng thay vào đó quy mô theo chiều rộng pixel. Nó sẽ nhắc người dùng về chiều rộng pixel mong muốn và thêm chiều rộng pixel đó vào đầu tên của tệp mới.

-- Prompt for an image
set theImageFile to choose file of type "public.image" with prompt "Please select an image:"
 
set dialogResult to (display dialog "Enter desired pixel width:" default answer "") try set pixelWidth to (text returned of dialogResult) as integer end try 
 
-- Locate an output folder
set theOutputFolder to (path to desktop folder as string)
 
-- Launch Image Events
tell application "Image Events"
    launch
 
    -- Open the image
    set theImage to open theImageFile
    tell theImage
 
        -- Determine a save name for the image
        set theName to name
        set theSaveName to (pixelWidth as text) & "-px-" & theName
 
        -- Scale the image to pixelWidth
        scale to size pixelWidth
 
        -- Save the image to the output folder, using the save name
        save as file type in (theOutputFolder & theSaveName)
 
        -- Close the image
        close
    end tell
end tell

5. Sao lưu các thư mục đến điểm đến đã chọn

Tập lệnh đơn giản này sao chép một thư mục đã chọn đến một điểm đến đã chọn, điều này có thể làm cho việc sao chép kéo và thả phức tạp bớt khó khăn hơn một chút.

set backupTarget to (choose folder with prompt "Select a Backup Target")
set backupDestination to (choose folder with prompt "Select a Backup Destination")
tell application "Finder"
    duplicate folder backupTarget to folder backupDestination
end tell

Kết luận

Để tìm hiểu thêm về AppleScript, bạn có thể xem tài liệu riêng của Apple. Vẫn tốt hơn là MacOSXAutomation.com, thân thiện hơn với noob.