0

I recently discovered applescript and i have been eager to make an internet bot with it. I've built my bot with a memory so it doesn't search the same website twice. I want to keep my memory on google docs so i don't have to carry around an adittional text edit file (plus i couldn't get applescript to use textedit because it kept timing out). I want to paste my data into the document when the application is stopped:

set the clipboard to data_memory_full           
tell application "Safari"     
    activate                 
    set URL of document 1 to --Google docs URL             
    tell application "System Events" to keystroke "v" using {command down}      
end tell

however when i do this the memory is always pasted into my applescript file and not safari. How do i stop this?! Thanks in advance

0

I don't fully grasp what it is you're trying to do, but I can say that you almost definitely don't want to be doing it by sending simulated keystrokes through System Events, or using System Events in any way to control processes. It's bad scripting, and those scripts will break in record time.

Your idea of using TextEdit was actually the better idea, as one can actually script TextEdit to edit a document directly, which is more reliable than your current approach.

But, even utilising TextEdit is actually unnecessary, as you can simply write data to a file directly from within AppleScript:


I'm going to assume that the variable data_memory_full contains plain text data, and that you'll likely want the resulting text file to use UTF-8 text encoding.

Given this, you can write the contents of the variable data_memory_full to a new or existing file like this:

set data_memory_full to "Some sample text to store in memory."


script memoryfile
    property sys : application "System Events"
    property path : "~/Google Drive/AppleScript/bot/MEMORY.data"
    property file : a reference to sys's file (my path)

    to make
        if not (my file exists) then ¬
            sys's (make new file ¬
            with properties {name:my path})
    end make

    to write textdata without overwriting
        local textdata, overwriting

        set textdata to textdata & linefeed

        if overwriting then set eof of ¬
            (my file as alias) to 0

        tell the current application ¬
            to write the textdata ¬
            to (my file as alias) ¬
            starting at eof ¬
            as «class utf8»
    end write

    to read
        tell the current application ¬
            to read (my file as alias) ¬
            as «class utf8»
    end read
end script


make memoryfile -- Create the data file if it doesn't already exist

-- This command commits data_memory_full to storage
tell the memoryfile to write data_memory_full without overwriting

read memoryfile -- This allows you to retrieve the file's contents

Change this line:

property path : "~/Google Drive/AppleScript/bot/MEMORY.data"

to contain the file path at where you wish the memory file to be created. The file need not exist already, as the script will create it if necessary. However, the folders comprising the path to where the file resides must each exist. In my tests, I created a file that was stored locally in my "Google Drive" directory, which then gets synchronised to Google Drive in the cloud (this requires you have installed Google's Backup & Sync software).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.