Tạo Quick Action chỉnh sửa nhanh kích thước ảnh trên macOS

Đầu tiên mở Automator, bạn có thể bấm command+space để tìm kiếm nhanh
Tiếp đó chọn Quick Action

Ở ô số 1 chọn vào [image files]
bấm vào ô search ở 2 tìm và chọn [Run AppleScript]
Dán vào script ở bên dưới này và Lưu lại ví dụ là [Resize1500px]

on run {input, parameters}
    repeat with theFile in input
        try
            -- Lấy đường dẫn đầy đủ của file
            set filePath to POSIX path of theFile
            
            -- Tách tên file và phần mở rộng
            set fileName to do shell script "basename " & quoted form of filePath
            set fileExtension to do shell script "echo " & quoted form of fileName & " | awk -F. '{print $NF}'"
            set fileNameWithoutExt to do shell script "basename " & quoted form of filePath & " ." & fileExtension
            
            -- Kiểm tra xem file có phải là ảnh không
            if fileExtension is in {"jpg", "jpeg", "png", "gif", "JPG", "JPEG", "PNG", "GIF"} then
                -- Tạo tên file mới với hậu tố resize1500
                set parentFolder to do shell script "dirname " & quoted form of filePath
                set newFilePath to parentFolder & "/" & fileNameWithoutExt & "resize1500." & fileExtension
                
                -- Tạo và thực thi lệnh sips để resize ảnh
                set maxWidth to 1500
                set cmd to "sips -Z " & maxWidth & " " & quoted form of filePath & " --out " & quoted form of newFilePath
                
                do shell script cmd
            else
                display dialog "File " & fileName & " không phải là file ảnh được hỗ trợ."
            end if
            
        on error errMsg
            display dialog "Lỗi khi xử lý file " & filePath & ": " & errMsg
        end try
    end repeat
    return input
end run

Chọn ảnh cần chỉnh kích thước và úm ba la

Kết quả, nhanh và gọn

Giải thích 1 chút về script bên trên

Lấy file ảnh được chọn sử dụng các lệnh shell để xử lý tên file chính xác hơn:
– basename để lấy tên file
– awk để tách phần mở rộngTạo tên file mới bằng cách thêm hậu tố “resize1500px”
Sử dụng lệnh sips của macOS để resize ảnh về bề ngang tối đa 1500px (tỷ lệ ảnh được giữ nguyên)
Lưu file mới vào cùng thư mục với file gốc

Related posts

Leave the first comment

0
Would love your thoughts, please comment.x
()
x