AppleScript实现蓝牙文件传输队列:检测传输状态并等待完成
Got it, let's tackle this Bluetooth file transfer queue problem for your AppleScript setup. I’ve dealt with similar automation hiccups before, so here’s a robust solution to make sure your files are sent one after another without conflicts:
The key is to avoid triggering direct transfers when a new file arrives. Instead, we’ll:
- Add incoming files to a persistent queue (so it survives script restarts)
- Run a background monitor that checks for active Bluetooth transfers, and processes the queue only when the coast is clear
1. Set Up Persistent Queue Storage
We’ll use a plist file to store the queue—AppleScript can read/write these easily, and they’re persistent across sessions.
-- Define where to store the queue (adjust this path if you prefer) set queuePlistPath to (path to library folder from user domain as text) & "Application Support:BluetoothTransferQueue.plist" -- Add a file to the queue on addToQueue(filePath) try set queueItems to readPlist(queuePlistPath) on error set queueItems to {} -- Initialize empty queue if plist doesn't exist yet end try set end of queueItems to filePath writePlist(queueItems, queuePlistPath) end addToQueue -- Helper: Read the queue from plist on readPlist(plistPath) return (do shell script "defaults read " & quoted form of POSIX path of plistPath & " 2>/dev/null || echo '()'") as list end readPlist -- Helper: Write the queue to plist on writePlist(plistData, plistPath) do shell script "defaults write " & quoted form of POSIX path of plistPath & " -array " & quoted form of (plistData as text) end writePlist
2. Detect Active Bluetooth Transfers
We’ll check if the system’s Bluetooth File Transfer agent is running—this tells us if a transfer is in progress.
-- Check if a Bluetooth transfer is currently active on isTransferInProgress() set runningProcesses to do shell script "pgrep -x 'Bluetooth File Transfer'" return (length of runningProcesses) > 0 end isTransferInProgress
3. Build the Queue Processor
This loop runs in the background, checking the queue and starting transfers only when nothing else is sending.
-- Process files in the queue one at a time on processQueue() repeat try set queueItems to readPlist(queuePlistPath) on error set queueItems to {} end try -- Only proceed if there are files in the queue and no transfer is running if (count of queueItems) > 0 and not isTransferInProgress() then set currentFile to item 1 of queueItems -- Remove the file from the queue before starting transfer set queueItems to items 2 through -1 of queueItems writePlist(queueItems, queuePlistPath) -- Run your actual Bluetooth transfer logic here transferFile(currentFile) end if -- Wait 2 seconds before checking again (adjust this delay as needed) delay 2 end repeat end processQueue -- Your existing transfer logic (replace with your actual send code) on transferFile(filePath) try tell application "Bluetooth File Transfer" activate -- Replace "Your Camera Name" with your actual Bluetooth device name/ID send file filePath to device "Your Camera Name" end tell -- Extra safety: Wait until the transfer finishes repeat while isTransferInProgress() delay 1 end repeat on error errMsg log "Transfer failed for " & filePath & ": " & errMsg -- Optional: Re-add the file to the queue if you want to retry failed transfers -- addToQueue(filePath) end try end transferFile
4. Integrate with Your Automation Trigger
Modify your existing script that runs when a file is added to ImagesPrint—instead of sending immediately, add the file to the queue and make sure the processor is running.
-- Trigger script (runs when a new file is added to ImagesPrint) on run {input} set newFilePath to POSIX path of (item 1 of input) -- Add the new file to the queue addToQueue(newFilePath) -- Start the queue processor if it's not already running set processorRunning to do shell script "pgrep -f 'BluetoothTransferQueueProcessor'" if (length of processorRunning) = 0 then do shell script "osascript -e 'tell application \"System Events\" to do shell script \"'\"'\"'\"osascript " & quoted form of POSIX path of (path to me as text) & " -e \"processQueue\"'\"'\"'\"' &> /dev/null &" end if end run
Key Tips for Reliability
- Device Targeting: Use
system_profiler SPBluetoothDataTypein Terminal to get your camera’s exact Bluetooth name/ID for thesend filecommand. - Error Handling: The
transferFilefunction includes basic error catching—uncomment the re-add line if you want to automatically retry failed transfers. - Queue Persistence: The plist file ensures your queue won’t get lost if your Mac restarts or the script crashes.
内容的提问来源于stack exchange,提问作者Max Kortge

