Skip to content

Instantly share code, notes, and snippets.

@tatarize
Last active April 10, 2023 21:19
Show Gist options
  • Save tatarize/875b870ff5cf96073bc9d895e8bf1b9f to your computer and use it in GitHub Desktop.
Save tatarize/875b870ff5cf96073bc9d895e8bf1b9f to your computer and use it in GitHub Desktop.

Spreadsheet-to-Keystrokes Script

This script is designed to automate the process of inputting data from a spreadsheet into a program that only allows input one field at a time. By reading an exported file and simulating keystrokes, the script inputs each cell value into the corresponding field in the program.

The script is simple and easy to use, with no complicated setup or configuration required. All you need to do is prepare the data in a tab-delimited text file, open the order entry system, and drag and drop the file onto the script, then select the first field in the program. The script will wait three seconds then automatically input the data for you.

Prerequisites

We are assuming you are running Microsoft Windows with VBScript (it comes standard). If this is inaccurate, a python example is also provided.

Assumptions

We are assuming that you can press tab to move to the next field, and that pressing enter will move to the next line.

Usage

  1. Open your spreadsheet program with the data formatted in the same order as the fields.
  2. Export the data as a text file. For example, in Microsoft Excel, Go to File -> Save As, and select text file. You do not want a string delimiter, and you want Tab delimited export.
  3. Place the script on your desktop with the csv file.
  4. Run the script by dragging the data-text file on to the sendkeys.vbs file (or python for non-windows environments).
  5. The script pauses for three seconds, use this time to select the first field in the program.
  6. Watch as it inputs the spreadsheet data into the program.

Exporting the file will give you 1 tab per cell. So move the cells over to correctly give you the number of tabs.

import time
import sys
import pyautogui
time.sleep(3)
file_name = sys.argv[1]
with open(file_name, 'r') as f:
contents = f.read()
pyautogui.typewrite(contents)
Dim WshShell, file_name, fso, ts, contents
WScript.Sleep 3000
file_name = WScript.Arguments(0)
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(file_name)
contents = ts.ReadAll
ts.Close
Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys contents
@tatarize
Copy link
Author

tatarize commented Apr 7, 2023

-- Get the path to the dropped file
on open dropped_items
    set file_path to (POSIX path of dropped_items)
    -- Pause for three seconds to allow time to switch to the correct application
    delay 3
    --display dialog file_path buttons {"Ok"}
    --delay 3
    tell application "System Events"
        -- Read the contents of the file
        set file_data to read file_path as «class utf8»
        
        -- Split the data into rows
        set row_data to paragraphs of file_data
        
        -- Remove the header row if present
        if item 1 of row_data contains tab then
            set row_data to items 2 thru -1 of row_data
        end if
        
        -- Loop through the rows and send keystrokes to the system
        repeat with this_row in row_data
            -- Split the row into individual fields
            set field_data to words of this_row
            
            -- Enter each field using the "keystroke" command
            repeat with this_field in field_data
                keystroke this_field
                keystroke tab
            end repeat
            
            -- Press "return" to submit the data
            keystroke return
            
        end repeat
    end tell
end open

This works when given proper authority.
The old code was probably fine in in Applescript 10 years ago. Give or take; but needed some minor changes to work in modern Applescript.

@tatarize
Copy link
Author

https://gist.github.com/tatarize/37f664e0240bc1548ad3a43c7acc2021

For the updated version:

{wait 500}$1{tab}$2{enter}$3{tab}{tab}$6{tab}{tab}{tab}{tab}{tab}{tab}

Should be the needed filename/ordering, assuming the final part needs 6 tabs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment