質問

I have created a droplet that reads a textfile and gives the content to clipboard:

on open theFile
open for access theFile
set fileContents to (read theFile)
close access theFile
return fileContents
set the clipboard to fileContents
end open

But i am looking for some code to take this contents from clipboard and insert it into the current field of the frontmost window of safari.

For this purpose i found some code used in an automator action that simulates somehow the keystrokes of cmd+v:

on run {input, parameters}
tell application "Safari"
    activate
    tell application "System Events"
        keystroke "v" using command down
    end tell
    return input
end tell
end run

My problem is how to put these two things together. At the end the droplet should work this way:

  • drop textfile on droplet
  • insert content of textfile in active field of frontmost safari window.

thanks for help

役に立ちましたか?

解決

This should get the job done for you.

I had the code keystroke a variable so you do not have to mess around with the clipboard and risk accidentally entering the wrong data.

on open the_file
    set the_file_Contents to (read the_file)
    tell application "Safari"
        activate
        tell application "System Events"
            keystroke the_file_Contents
        end tell
    end tell
end open

If you wanted the code to use the clipboard still (more efficient for large text files), the code would look like this.

on open the_file
    set the clipboard to (read the_file)
    tell application "Safari"
        activate
        tell application "System Events"
            keystroke "v" using command down
        end tell
    end tell
end open
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top