Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Automate Data Extraction on macOS Using AppleScript and Terminal

Batch extraction typically refers to the process of extracting data from multiple files or sources in an automated manner. While the term "batch extraction" is often associated with Windows Batch scripts, macOS users can achieve similar functionality using AppleScript, Automator, and Terminal commands. This article will guide you through the process of automating data extraction on macOS, highlighting the importance of such automation for efficiency and accuracy in data handling tasks.


Examples:


Example 1: Extracting Text from Multiple Files Using AppleScript


AppleScript can be used to automate the extraction of specific text from multiple files. Below is an example script that extracts lines containing a specific keyword from all text files in a directory.


set keyword to "search_term"
set sourceFolder to choose folder with prompt "Select the folder containing the text files:"
set destinationFile to (choose file name with prompt "Save extracted data as:" default name "extracted_data.txt")

tell application "Finder"
set fileList to (files of sourceFolder whose name extension is "txt")
end tell

set extractedData to ""

repeat with aFile in fileList
set filePath to (aFile as text)
set fileContents to read file filePath
set fileLines to paragraphs of fileContents

repeat with aLine in fileLines
if aLine contains keyword then
set extractedData to extractedData & aLine & linefeed
end if
end repeat
end repeat

try
set outputFile to open for access destinationFile with write permission
write extractedData to outputFile
close access outputFile
on error
try
close access destinationFile
end try
end try

Example 2: Using Terminal for Batch Extraction with Shell Scripts


For users comfortable with the command line, shell scripts can be used to perform batch extraction tasks. Below is a shell script that extracts lines containing a specific keyword from all text files in a directory and writes them to a single output file.


1. Open Terminal.
2. Create a new shell script file:


touch extract_data.sh
chmod +x extract_data.sh

3. Edit the script using a text editor like nano:


nano extract_data.sh

4. Add the following script to extract_data.sh:


#!/bin/bash

keyword="search_term"
source_folder="/path/to/source/folder"
destination_file="/path/to/destination/extracted_data.txt"

> "$destination_file"

for file in "$source_folder"/*.txt; do
grep "$keyword" "$file" >> "$destination_file"
done

5. Save and close the editor (Ctrl + X, then Y, then Enter).
6. Run the script:


./extract_data.sh

Example 3: Automator Workflow for Batch Extraction


Automator is a powerful tool on macOS for creating workflows to automate repetitive tasks. Here's how to create an Automator workflow for extracting data:


1. Open Automator and create a new workflow.
2. Add the "Get Specified Finder Items" action and select the files you want to process.
3. Add the "Run Shell Script" action and use the following script:


keyword="search_term"
destination_file="$HOME/Desktop/extracted_data.txt"

> "$destination_file"

for file in "$@"; do
grep "$keyword" "$file" >> "$destination_file"
done

4. Save the workflow and run it to extract the data.


To share Download PDF