AppleScript多行文本取值循环异常及范围选择技术咨询
Hey there! Let’s break down your AppleScript questions step by step— I’ve dealt with those frustrating "works once then glitches" bugs before, so I know exactly where you’re coming from.
1. Fixing the "Works on First Compile, Breaks When Re-Running Without Compiling" Issue
This usually happens because persistent global variables retain their values from the last run when you execute an uncompiled script. Script Editor doesn’t reset these variables automatically between uncompiled runs, which throws off your logic (like appending to a list that still has old data from a previous execution).
Solution: Reset Variables Explicitly
Always initialize your variables at the start of the script to ensure a clean state every time it runs. Here’s a fixed example of a script that grabs multi-line text, stores lines in a variable, and loops until the end:
-- Start with a fresh empty list every run set myTextList to {} -- Example: Grab multi-line text from TextEdit tell application "TextEdit" set inputText to text of front document end tell -- Split text into individual lines and loop through each set textLines to paragraphs of inputText repeat with currentLine in textLines -- Skip empty lines if needed if currentLine is not equal to "" then set end of myTextList to currentLine -- Add your custom processing here (e.g., log the line) log "Processed line: " & currentLine end if end repeat -- Verify the final result log "Final list of lines: " & myTextList
By resetting myTextList to {} at the start, you ensure it never has leftover data from previous runs— even when you re-run without compiling.
2. Selecting a Row/Column Range & Showing a Prompt If Nothing’s Selected
How you handle ranges depends on the app you’re targeting (e.g., Numbers, Excel, or a text editor). Let’s use Numbers as an example since you mentioned rows/columns:
Script to Check for Selected Cells
tell application "Numbers" activate set targetSheet to active sheet of front document -- Check if any cells are selected if selection is empty then display alert "Oops!" message "Please select a row, column, or range of cells first." buttons {"OK"} default button 1 return -- Exit the script immediately if no selection end if -- Capture the selected range for later use set selectedRange to selection log "Selected range address: " & address of selectedRange end tell
For text editors like TextEdit (selecting lines instead of cells), adjust the check to look for empty selected text:
tell application "TextEdit" if selected text of front document is "" then display alert "No text selected!" message "Please highlight a range of lines to process." return end if end tell
3. Looping Through a Selected Range
Once you’ve confirmed a range is selected, you can loop through its contents— either by row, column, or individual cells. Here’s how to do it in Numbers:
Loop Through Each Row in the Selected Range
tell application "Numbers" set selectedRange to selection set rangeRows to rows of selectedRange repeat with currentRow in rangeRows -- Get all cells in the current row set rowCells to cells of currentRow repeat with currentCell in rowCells -- Process each cell's value (skip empty cells if needed) set cellValue to value of currentCell if cellValue is not missing value then log "Cell value: " & cellValue -- Add your custom logic here (e.g., format the value) end if end repeat end repeat end tell
Loop Through Selected Text Lines in TextEdit
tell application "TextEdit" set selectedText to selected text of front document set selectedLines to paragraphs of selectedText repeat with lineText in selectedLines if lineText is not equal to "" then -- Example processing: Convert line to uppercase set modifiedLine to uppercase of lineText log "Modified line: " & modifiedLine end if end repeat end tell
内容的提问来源于stack exchange,提问作者s.sasenharn

