Computer >> 컴퓨터 >  >> 체계 >> MAC

5 생산성 향상을 위한 Mac의 Applescript 사용

5 생산성 향상을 위한 Mac의 Applescript 사용

Applescript는 Apple의 다소 모호한 스크립팅 언어이지만 초보자 프로그램이 활용할 수 있는 강력한 도구입니다. 성가신 작업을 처리하는 몇 가지 영리한 Applescript를 사용하면 생산성을 높이고 우울을 자동화할 수 있습니다.

Applescript란 무엇입니까?

5 생산성 향상을 위한 Mac의 Applescript 사용

Applescript는 Finder, iTunes, QuickTime 및 Mail과 같은 대부분의 Mac 응용 프로그램과 인터페이스합니다. Automator에 익숙하다면 Applescript는 해당 응용 프로그램의 일종의 고급 사용자 버전입니다.

1. 숨김 파일 전환

이것을 응용 프로그램으로 저장하면 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. 파일 이름 일괄 변경

이 스크립트는 사용자에게 파일 이름을 묻는 메시지를 표시한 다음 해당 텍스트 문자열과 증분 인덱스를 사용하여 선택한 파일의 이름을 자동으로 바꿉니다. 파일 1부터 10까지 앞에 0을 추가하는 데 도움이 됩니다.

-- 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. 백분율로 이미지 크기 조정

이 스크립트는 이미지를 원래 크기의 50%로 조정합니다.

-- 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. 이미지를 픽셀 너비로 조정

이것은 이전 스크립트의 시작 부분을 많이 사용하지만 대신 픽셀 너비로 조정됩니다. 사용자에게 원하는 픽셀 너비를 묻고 해당 픽셀 너비를 새 파일 이름의 시작 부분에 추가합니다.

-- 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. 선택한 대상에 폴더 백업

이 간단한 스크립트는 선택한 폴더를 선택한 대상에 복제하므로 복잡한 드래그 앤 드롭 복사를 조금 덜 고통스럽게 만들 수 있습니다.

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

결론

AppleScript에 대해 자세히 알아보려면 Apple 자체 설명서를 확인하세요. 더 나은 것은 MacOSXAutomation.com이며, 이는 멍청한 놈에게 더 친숙합니다.