You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过AppleScript或Terminal正确获取多文件路径?

Handling Multiple Selected Finder Items in Your Script

Got it, let's get your script working with multiple selected Finder items! Your current setup works great for single files, but we can tweak it to capture all selected paths so you can run batch operations like move or delete.

Solution with AppleScript (Core Logic)

AppleScript is the go-to for interacting with Finder, and it's easy to adjust to handle selections of any size. Here's a script that grabs every selected item's POSIX path (the format you're using, like /Users/lawsome/Desktop/jan-study/untitled folder/):

tell application "Finder"
    -- Get all selected items
    set selectedItems to selection
    
    -- Exit early if nothing's selected
    if selectedItems is {} then
        display alert "Oops! No items selected in Finder."
        return
    end if
    
    -- Build a list of POSIX paths
    set itemPaths to {}
    repeat with anItem in selectedItems
        -- Convert each item to a POSIX path (add single quotes if you need them for shell commands)
        set end of itemPaths to POSIX path of (anItem as alias)
        -- Uncomment below if you want paths wrapped in single quotes like your original example:
        -- set end of itemPaths to "'" & POSIX path of (anItem as alias) & "'"
    end repeat
end tell

-- Example: Use the paths for batch operations
-- This loop logs each path (replace with your move/delete logic)
repeat with aPath in itemPaths
    log aPath
end repeat

Key Details:

  • selection grabs every item you've highlighted in Finder, not just one.
  • The repeat loop iterates over each selected item, converting it to a POSIX path (compatible with shell commands and file operations).
  • If you need paths wrapped in single quotes (like your original output), uncomment the line that adds ' around the path—this avoids issues with spaces in folder/file names.

Using the Paths in a Shell Script

If you're running shell commands (like mv or rm) after grabbing the paths, here's how to integrate the AppleScript logic into a bash script:

#!/bin/bash

# Fetch selected Finder paths using AppleScript
finder_paths=$(osascript -e 'tell application "Finder" to set selectedItems to selection' \
                        -e 'if selectedItems is {} then return ""' \
                        -e 'set itemPaths to {}' \
                        -e 'repeat with anItem in selectedItems' \
                        -e 'set end of itemPaths to POSIX path of (anItem as alias)' \
                        -e 'end repeat' \
                        -e 'return itemPaths as text')

# Split the returned string into an array (paths are separated by newlines)
IFS=$'\n' read -r -d '' -a paths_array <<< "$finder_paths"

# Example: Batch move all selected items to a target folder
target_directory="/Users/lawsome/Desktop/processed_files"
mkdir -p "$target_directory"  # Create folder if it doesn't exist

for path in "${paths_array[@]}"; do
    echo "Moving: $path"
    mv "$path" "$target_directory/"
done

# Example: Batch delete (uncomment if needed)
# for path in "${paths_array[@]}"; do
#     rm -rf "$path"
# done

How This Works:

  • The osascript command runs the AppleScript logic and returns all paths as a newline-separated string.
  • We split that string into a bash array so we can loop through each path safely (even with spaces in names).
  • The example includes a batch move operation—swap that with rm or other commands for your needs.

内容的提问来源于stack exchange,提问作者lawsome

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:40:26