• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

JSX + AppleScript / custom arguments from InDesign

Contributor ,
Jul 15, 2024 Jul 15, 2024

Copy link to clipboard

Copied

Hi,
 
I am trying to pass parameters from InDesign jsx to AppleScript to open up a url with some custom handling based on the user's default browser.
 
I have this code arrangement so far:

 

 

var scriptFile = new File($.fileName); // or new File(app.activeScript)
var folderPath = scriptFile.path;
var myscriptPath = folderPath + "/mycustom.scpt";
var myscriptFile = File(myscriptPath);
var myargs=["Hello "];
var myargsString=myargs.join(",");
var script = 'set scriptFile to POSIX file "' + myscriptFile.fsName + '"\n' + 'set myargs to {' + '"' + myargsString + '"' + '}\n' +
'run script scriptFile with parameters myargs'; 
app.doScript(script,ScriptLanguage.APPLESCRIPT_LANGUAGE);

 

 

 
 On the AppleScript side:

 

 

use framework "Cocoa"
use AppleScript version "2.4"
use scripting additions

property NSURL : a reference to current application's NSURL
property NSWorkspace : a reference to current application's NSWorkspace
property NSFileManager : a reference to current application's NSFileManager

on run {myarg}
    set any_url to "http://www.apple.com"

    set the_url to NSURL's URLWithString:any_url
    set theApp to (NSWorkspace's sharedWorkspace()'s URLForApplicationToOpenURL:the_url)
    if theApp is not missing value then
        -- Get the display name of the application
        set appPath to theApp's |path|()
        set browserName to (NSFileManager's defaultManager()'s displayNameAtPath:appPath)
        display dialog (browserName as text)
    else
        display dialog "No application found to open the URL."
    end if
end run

 

 

 

I get this error:

asaxena_0-1721110963350.png

 

1. What is the reason for this error and how to fix it?

2. Is there a better way to pass custom arguments from jsx to AppleScript and how to extract the arguments on AppleScript side?

 

Thanks

 

 

TOPICS
How to , Scripting , SDK

Views

298

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jul 17, 2024 Jul 17, 2024

Hi @asaxena , I would look at @Kasyan Servetsky ’s suggestion and see if you can avoid AppleScript.

 

Your JS code isn’t passing an argument for the URL over to the Applecript—the missing 3rd parameter of app.doScript(). Something like this works for me:

 

The JS, which reads a text .applescript file named LoadDefaultBrowser saved to my desktop:

 

 

 

//set the url to send to Applescript
var args = ["https://www.google.com"];

//applescript saved as a text file (.applescript)
var as = readFile(F
...

Votes

Translate

Translate
Guru ,
Jul 16, 2024 Jul 16, 2024

Copy link to clipboard

Copied

I suggest a much simpler solution. I used it in this script.

When the user clicks the About button, the corresponding URL address is opened in the default browser.

function JumpToLink() {
	var tmpFolder = new Folder(Folder.temp.absoluteURI + "/Kasyan");
	tmpFolder.create();
	var linkJumper = File(tmpFolder.absoluteURI + "/" + tmpFileName + "temp.html");
	
	if (!linkJumper.exists) {
		linkJumper.open("w");
		var linkBody = '<html><head><META HTTP-EQUIV=Refresh CONTENT="0; URL=http://kasyan.ho.ua/indesign/text/find_text_in_location/find_text_in_location.html"></head><body> <p></body></html>'
		linkJumper.write(linkBody);
		linkJumper.close();
	}

	linkJumper.execute();
}

 

A temporary HTML file is created in a  temporary folder and triggered by the execute() command in the default app (e.g. Fireworks on my computer).

— Kas

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 16, 2024 Jul 16, 2024

Copy link to clipboard

Copied

For the AppleScript part, all you need is this:

open location yourURL

 (which, I guess, you can just call as string directly from JS).

 

This will open the URL in the default app. (If you pass some completely invalid URL such as "some irrelevant string" this command fails without returning any error though).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 17, 2024 Jul 17, 2024

Copy link to clipboard

Copied

Apologies. Perhaps I wasn't very clear. My requirement is to detect the default browser and take some necessary action accordingly on the applescript. So, if the default browser is "Google Chrome" do some action, if it is "Safari" do some other custom action etc.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 17, 2024 Jul 17, 2024

Copy link to clipboard

Copied

LATEST

Hi @asaxena , I would look at @Kasyan Servetsky ’s suggestion and see if you can avoid AppleScript.

 

Your JS code isn’t passing an argument for the URL over to the Applecript—the missing 3rd parameter of app.doScript(). Something like this works for me:

 

The JS, which reads a text .applescript file named LoadDefaultBrowser saved to my desktop:

 

 

 

//set the url to send to Applescript
var args = ["https://www.google.com"];

//applescript saved as a text file (.applescript)
var as = readFile(Folder.desktop + "/LoadDefaultBrowser.applescript")
//3rd parameter is an array of items sent to applescript 
app.doScript(as, ScriptLanguage.applescriptLanguage, args);

function readFile(p) {
	var f = new File(p);
	f.open("r");  
	var x = f.read(); 
	f.close();
	return x;
}

 

 

 

The LoadDefaultBrowser.applescript to activate the default browser:

 

 

 

set myurl to item 1 of arguments

set browse to paragraph 1 of (do shell script "defaults read \\
    ~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure \\
    | awk -F'\"' '/http;|https;/{print window[(NR)-1]}{window[NR]=$2}'")

if browse is "" or browse contains "safari" then
	tell application "Safari"
		activate
		make new document with properties {URL:myurl}
	end tell
else if browse contains "chrome" then
	tell application "Google Chrome"
		activate
		open location myurl
	end tell
else if browse contains "firefox" then
	tell application "Firefox"
		activate
		delay 1
		open location myurl
	end tell
else
	display dialog "Browser not found"
end if

 

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines