Selecting and Copying text on web page Help!

Hi guys I have 1 last step to make my macro fully automated.

I recorded a quick screencast to show you what I am trying to do.

I need KM to select the same text from the same cell in table (safari)

so that I can make this macro fully automated

http://d.jon.am/1ajm6+

I have solved similar issues with two GUI-dependent methods (and note that GUI-dependence results in less-than robust reliability):
• Use KM’s “Found Image” set to the column header, and click (or double-click?) at the vertical offset you want.
• Count the {tabs} from a freshly-loaded page that takes you to the selection you want, and run an Action that repeats {tab} that many times. For this to work, I think you get better results with "Safari ▹ Preferences ▹ Advanced ▹ Accessibility ▹ Press Tab to … " checked.

You might look into repurposing the script that was recently provided by Chris that gets the page source and searches it by regex.

And you might look into ShortCat (which I don’t use but would love to).


[Edit 2018/04/09 03:01 CST — See my post #4 on this page for updated information.]


Hey Jon,

First let me congratulate you on providing a vid, so we could see EXACTLY what you were doing.  :smile:

This saves everyone time and effort.

If you don’t have BBEdit then please download its freeware sibling TextWrangler, so you have a scriptable text editor on your system.

Then run this AppleScript from the Script Editor app:

tell application "Safari"
  tell front document
    set _text to its text
  end tell
end tell

tell application "TextWrangler"
  activate
  make new text document with properties {text:_text}
end tell

See what you get.

Sometimes the text is easily parsed – sometimes no – especially in the case of tables.

Chances are pretty good that this method will work, since your target cell is right after the table header.

Run it against 3 or 4 of your pages and see if the output is regular.

If it is then post it, so we can help you parse it – or you can send it to me direct if you want to keep it more private. (listmeister@thestoneforge.com)

From there we’ll probably use a regular expression to grab the number.

You should also peek at the source of the page, because it might be even easier to parse.

tell application "Safari"
  tell front document
    set pgSrc to its source
  end tell
end tell

tell application "TextWrangler"
  activate
  make new text document with properties {text:pgSrc}
end tell

-Chris

2 Likes

Hey Folks,

TextWrangler is no longer being developed — see my post on the subject linked below.

FYI – TextWrangler Fading into the Sunset – BBEdit-Lite Taking the Stage Again

-ccs

1 Like

Hey Folks,

I should have mentioned how to transition the TextWrangler AppleScript to BBEdit.

Very simply:

tell application "BBEdit"
   activate
   make new text document with properties {text:pgSrc}
end tell

-Chris

Hi every KM user,

If one would expand the above script and let BBEdit save the new document with the title, how would one do it?

tell application "Safari"
   tell front document
      set _text to its text
      set _title to its title
   end tell
end tell

tell application "BBEdit"
   activate
   make new text document with properties {text:_text}
   save text document with _title
   save text document to "~/Documents"
end tell

Of course this could be done in several ways, but how?

/
okn

Hey Omar,

It just takes the correct syntax.

Safari doesn’t know what a “title” is – it requires a “name”. (This syntax is easy to discover.)

BBEdit doesn’t understand tilde-based paths.

BBEdit’s save-to syntax is a little more difficult to figure out IF you don’t understand what an alias or an HFS path is.

save reference ¬
     to alias ¬
     saving as stationery boolean ¬
     add to recent list boolean

Alias means a full path to the saved file.

The last two items are optional, as you can see in BBEdit scripting dictionary in the Script Editor.app or Script Debugger.

----------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2018/04/19 11:30
# dMod: 2018/04/19 11:37 
# Appl: BBEdit, Safari
# Task: Write Text of front Safari Web Page to a file in the Finder using BBEdit.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @BBEdit, @Safari, @Write, @Text, @Front, @Safari, @Web, @Page, @File
----------------------------------------------------------------

tell application "Safari"
   tell front document
      set safariText to its text
      set safariTitle to its name
   end tell
end tell

----------------------------------------------------------------

set saveDir to (path to documents folder as text)
set fileSuffix to ".txt"

tell application "BBEdit"
   activate
   set newDoc to make new text document with properties {text:safariText}
   save newDoc to saveDir & safariTitle & fileSuffix
end tell

----------------------------------------------------------------

I’ve used an HFS path in the script above, because I like to use anchored-aliases when building path strings.

BBEdit will also accept a full POSIX Path to the file to be saved.

Here’s one way to construct a full POSIX Path using a tilde-path as a starting point.

----------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2018/04/19 12:04
# dMod: 2018/04/19 12:10
# Appl: System Events, BBEdit
# Task: Using System Events to expand a tilde-path.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @BBEdit, @System_Events, @Expand, @Tilde-Path, @Path
----------------------------------------------------------------

# Target Save Directory.
set saveDir to "~/Documents/BBEdit Documents/"

# Expand Tilde-Path with System Events.
tell application "System Events" to set saveDir to POSIX path of disk item saveDir

# Collate entire save path with document name and file extension.
set documentTitleName to "YourDocName"
set documentSuffix to ".txt"
set fullSavePath to saveDir & documentTitleName & documentSuffix

# Save Front Document to “fullSavePath” with BBEdit.
tell application "BBEdit" to save front document to fullSavePath

----------------------------------------------------------------

Here’s how to do the same thing using AppleScriptObjC instead of System Events.

----------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2018/04/19 12:24
# dMod: 2018/04/19 12:31
# Appl: AppleScriptObjC, BBEdit
# Task: Expand a tilde-path using AppleScriptObjC.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @BBEdit, @System_Events, @Expand, @Tilde-Path, @Path
----------------------------------------------------------------
use AppleScript version "2.4" -- Yosemite (10.10) and later
use framework "Foundation"
use scripting additions
----------------------------------------------------------------

# Target Save Directory.
set saveDir to "~/Documents/BBEdit Documents/"

# Expand Tilde-Path with AppleScriptObjC.
set saveDir to ((current application's NSString's stringWithString:saveDir)'s stringByExpandingTildeInPath) as text

# Collate entire save path with document name and file extension.
set documentTitleName to "YourDocName"
set documentSuffix to ".txt"
set fullSavePath to saveDir & documentTitleName & documentSuffix

# Save Front Document to “fullSavePath” with BBEdit.
tell application "BBEdit" to save front document to fullSavePath

----------------------------------------------------------------

-Chris

1 Like

3 posts were split to a new topic: How Do I Add New Macros to Best Macro List