I'm building a service through Automator. So I'm trying to execute this bit of Javascript in Chrome and set the clipboard to the value of one of the variables. Essentially it is grabbing the contents of an element which is a file path. Ex: id:file:///Volumes/Imaging Share/TENNIS_2018/TW-07.23.18-[54]-EleVen_Fila_Womens_Model/Capture/CaptureOne/Settings100/TWCES-GY-17057.CR2.cos

stripping "id:file//" and then saving the remaining string in a variable or clipboard.

The Javascript works if I execute it in the console but I'm not having any luck getting the Applescript to execute it and not quite sure how to get the value of the variable so it can be used later. here is the javascript by its self

var link = document.getElementById('q');
var str = link.value.replace('id:file://', '');

here it is in the context of the Applescript which is not working

tell application "Google Chrome" to activate
tell application "Google Chrome"
    execute javascript ("var link = document.getElementById('q');
    var str = link.value.replace('id:file://', '');
    document.execCommand('copy'); ")
end tell

Any help will be much appreciated.

  • You cannot execute javascript without telling it where it's going to be executed, i.e., use a tell block, e.g., after tell application "Google Chrome", use tell active tab of front window, then your execute javascript ... and don't forget to close the tell block with another end tell. Or other proper form of addressing the target where your want to execute javascript ..., etc.. – user3439894 Aug 3 '18 at 3:23
  • Awesome that worked perfectly. now I just need to figure out how to pass the value of str back to Applescript or to the clipboard – Vasily Sidorenko Aug 3 '18 at 19:00
  • Do set str to execute in active tab of the front window JavaScript "…". Or set the clipboard to.... – CJK Aug 4 '18 at 11:25

You must log in to answer this question.

Browse other questions tagged .