From 18d3e38e3f3011f5b6456aad164cd995a2538920 Mon Sep 17 00:00:00 2001 From: Andrew Hundt Date: Thu, 30 Apr 2026 13:45:15 -0400 Subject: [PATCH] Fix space-to-0 bug and missing app icon - Fix bug where spaces were typed as '0' due to AppleScript coercing space character to integer 0, selecting numpad key code 82 - Handle whitespace (space, tab, return) with explicit hardware key codes - Guard digit key code lookup to only run on actual digit characters - Add error handling for empty/inaccessible clipboard - Raise character limit from 250 to 500 - Remove Assets.car in build.sh so custom icon displays correctly --- build.sh | 1 + main.scpt | 57 ++++++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/build.sh b/build.sh index 18c14c3..b1f0d3c 100755 --- a/build.sh +++ b/build.sh @@ -11,6 +11,7 @@ osacompile -o "$APP_NAME" -x "main.scpt" /usr/libexec/PlistBuddy -c "Add NSHumanReadableCopyright String '© 2015 https://github.com/EugeneDae/Force-Paste/'" "$INFO_PLIST" rm -f "$APP_NAME/Contents/Resources/applet.icns" +rm -f "$APP_NAME/Contents/Resources/Assets.car" cp "icon.icns" "$APP_NAME/Contents/Resources/" if [ -f "Force-Paste.zip" ]; then diff --git a/main.scpt b/main.scpt index f10d3bf..1d4e773 100644 --- a/main.scpt +++ b/main.scpt @@ -1,15 +1,42 @@ -set numbers_key_codes to {82, 83, 84, 85, 86, 87, 88, 89, 91, 92} - -set input to do shell script "pbpaste" - -if (input is not missing value and length of input is less than 250) then - tell application "System Events" - repeat with char in the characters of input - try - key code numbers_key_codes's item (char + 1) - on error - keystroke char - end try - end repeat - end tell -end if +-- Force-Paste: Optimized and Robust Character Handling +-- Addresses "0 instead of space" bug and improves layout compatibility. + +set digitCodes to {82, 83, 84, 85, 86, 87, 88, 89, 91, 92} -- Numpad 0-9 codes + +try + set clipboardContent to do shell script "pbpaste" +on error + return -- Exit if clipboard is empty or inaccessible +end try + +if (clipboardContent is not missing value and length of clipboardContent > 0) then + -- Cap length at 500 to prevent system hang while maintaining utility + if length of clipboardContent > 500 then + set clipboardContent to text 1 thru 500 of clipboardContent + end if + + tell application "System Events" + repeat with char in characters of clipboardContent + set c to contents of char + + -- 1. Structural White Space (Hardware-level Key Codes) + -- This bypasses software layout mapping which often fails in AppleScript + if c is " " then + key code 49 -- Space + else if c is tab then + key code 48 -- Tab + else if c is return or c is linefeed then + key code 36 -- Return + + -- 2. Numeric Digits (Explicit check to prevent math errors) + else if c is in "0123456789" then + set digitIdx to (c as integer) + 1 + key code (item digitIdx of digitCodes) + + -- 3. Standard Characters (Letters, Symbols, Emojis) + else + keystroke c + end if + end repeat + end tell +end if \ No newline at end of file