Using AppleScript to copy a webpage TITLE to clipboard - applescript

I am looking to copy the webpage title (of an open webpage in safari) to the clipboard using apple script (or a javascript snippet will do).
I will then use that Variable in Automator.
I have looked all over the web, but cannot seem to find the answer.

I just tried this with Safari and it seems to work as expected:
tell application "Safari"
get name of document 1
end tell

Related

Trying to click a website button with AppleScript

I wonder if anyone can please help me.
I am trying to use AppleScript to click the buy now button on
https://www.amazon.co.uk/Guinness-World-Records-2022-1/dp/1913484114/ref=zg-bs_books_1/261-5455843-9162427?pd_rd_w=vedUN&pf_rd_p=c3077bff-a471-42bf-a406-b93ec8e1a044&pf_rd_r=G24WF6JCCS60QFBJ54J3&pd_rd_r=b3faca64-d7e3-44a1-9bf8-0c454278bdff&pd_rd_wg=KvFlN&pd_rd_i=1913484114&psc=1
with Safari.
I am very new to all of this but I have spent hours pouring over guides and help with no success.
I have used 'inspect' to find the ID of the button which it says is 'Buy-Now-Button'
So that it is easy for me to test I already have the product page of what I want to buy open in Safari in the background.
Then I run the following script:
tell application "Safari"
activate
delay 2
do JavaScript "document.getElementById'buy-now-button').click();" in document 1
end tell
When I run this script it switches to Safari then nothing happens. No error messages or anything just nothing. I have tried it by the name rather than the ID but I just don't know what is going wrong.
Could anyone help please?
You are missing the opening ( in, document.getElementById'buy-now-button'):
do JavaScript "document.getElementById'buy-now-button').click();" in document 1
So, the following two examples work for me:
tell application "Safari" to do JavaScript "document.getElementById('buy-now-button').click();" in document 1
And:
tell application "Safari" to do JavaScript "document.getElementsByClassName('a-button-input')[1].click();" in document 1

Opening a Keynote Presentation using AppleScript (Keynote 6.5.2)

I am trying to see if i can change the text of some slides inside a keynote presentation that has already been created but i am having problems just opening it to begin with.
here is my current simple applescript:
set thisFile to "Users/myUserName/Desktop/KeynoteAppleScript.key"
tell application "Keynote"
activate
open thisFile
show slide 1
end tell
I get an error stating that the file I am trying to open :
"Users/myUserName/Desktop/KeynoteAppleScript.key" couldnt be opened
because there is no such file".
Thats obviously not correct, it is there and the i have double checked the name of the file to verify.
I am using Keynote 6.5.2
Script Editor Version 2.7, AppleScript 2.4
What am I doing wrong here?
Try adding a leading fwd slash to the path
e.g,
set thisFile to "/Users/myUserName/Desktop/KeynoteAppleScript.key"
I needed to do this to get it opening on 10.8, Keynote 5.3
ALSO..
When in doubt with a file path, try dragging the file from the desktop to the script window and it will insert a correct path.

Increase speed of applescript keystroke

I'm using applescript to automate some browser activity. I have it tied to the speech recognition, so that I can make some custom voice commands.
One such command is "I wanna go home", which when heard, pulls up the relevant bus schedule on my browser. Given that the public transit system uses PHP and GET, the URL gets pretty long. Therefore, the keystroke myURL in the applescript takes a while to execute (something like 1-2 seconds). While I can live with losing 1-2 seconds, I'd really rather not.
With that in mind, is it possible to send these keystrokes faster? I believe I read somewhere that using key code is faster than using keystroke, but for the life of me, I can't figure out where I read that.
EDIT: My browser of choice is Google Chrome, which I couldn't find URL hooks for. This is why I had to resort to using keystrokes. Therefore, I'd prefer answers that work with Chrome over Safari
Another way to solve this is to put the text in the clipboard, then paste it. You can save the clipboard contents first and put it back afterward, so you don't lose what's already there. This method works in other situations when you want to enter a lot of text, not just for browser URLs like the other answers.
set clipboard_contents to (the clipboard)
set the clipboard to "Some long string that would take a while with the keystroke method"
tell application "System Events" to keystroke "v" using command down
delay 0.2 -- needed because otherwise the next command can run before the paste occurs
set the clipboard to clipboard_contents
I'm pretty sure you can script your browser to open the URL directly, instead of typing in the keystrokes (faking input events in AppleScript should be considered a last resort).
You can do this with the open location standard addition:
open location "http://google.com"
Open location is defined in Standard Additions and should not be enclosed in a tell statement.
open location "http://google.com"
If you want to target a specific browser you can use:
tell application "Safari"
if not (exists document 1) then reopen
set URL of document 1 to "http://google.com"
end tell
To target Chrome:
tell application "Google Chrome"
if not (exists window 1) then reopen
set URL of active tab of window 1 to "https://www.google.com/"
end tell

AppleScript Transmit Script for uploading file to replace itself on web server

So I have this idea for a handy little AppleScript which in my opinion would be very handy in speeding up the process of uploading a local file, to its same location on the server.
In other words, you have to specify the home folder on the server and locally, but once that's finished, it would be nice to just press like "Command" + "Shift" "U" for upload or some other hot key combination not in use by OS X for uploading the currently selected file in the Finder.
I find myself needing to do this a lot, and it will save a lot of time!
Someone please tell me if there is an easier way to do this, but I think this will be a good learning experience on top of it all.
I need some help on how I should get started however..
1) the command line program curl can upload files. 2) if you have a file selected in a Finder window applescript can get the selection. Using those 2 ideas you can automate your task. I don't know the exact curl command but that should be easy to find using google. So you select a file in the Finder and then run the script. The script can be run with a keyboard shortcut as you mentioned or just put it in the Script menu and run it from there.
tell application "Finder"
set selectedFile to item 1 of (get selection)
set selectedFile to selectedFile as text
end tell
do shell script "curl -switchesToUpload " & quoted form of POSIX path of selectedFile
I use Cyberduck which is an ftp client you can set it up so when you double click a file on the server it opens it up in your favorite editor.( textmate is my favorite.) it atuomagiclly downloads and uploads when you save.
this seems like a much better solution to the problem

What applescript will a particular application accept?

I see a lot of applications that can respond to applescript messages such as this one.
tell application "Safari"
return URL of front document as string
end tell
How can I query an application to find out what commands it will accept? Is there a tool for doing so?
In AppleScript Editor, do "Open Dictionary", select the application you are interested in, and you get a nice browser that shows the commands and their documentation (if the developers have chosen to provide any).

Resources