Bash and applescript


I may call myself a applescript newbie but I am dumb as far as bash scripting is concerned.

I have two questions:

  1. What is the most reliable way to retrieve the current web page URL from Firefox?
    I noticed some issues here:

Currently I am using:

tell application "Firefox"
	set myFirefox to properties of front window as list
	set currentURL to item 3 of contents of myFirefox
	return result
	end tell

It works most of the time but sometimes (as mentioned in the web pages above) I also get the error

  1. You will laugh at my newbieness now!!

I am currently doing this:
I have a hotkey trigger (Cmd+Option+P) set for:

do shell script “sh /Users/chris/Desktop/”

This is

(In the above code “| qs” is used to send the output of the script firefox.scpt to Quicksilver. The keystroke statement that follows is used to use the action paste. So when I use the hotkey to launch do shell script “sh /Users/chris/Desktop/”, the firefox URL gets pasted in the active application—no matter what the application is as long as it accepts text input in the place where my keyboard cursor is)
The firefox.scpt is the script that I mentioned in my question 1.

So basically, the purpose of my script is to instantaneously paste the current URL in firefox to any application without having to do anything except press the hotkey(Cmd+Option+P).

How can I make the above three scripts into one script or do you have any better ideas? Please note that I want to do all this using Quicksilver only because that way I will be able to apply the same logic to many other actions in Quicksilver.



according to your posted link, this is the most reliable way to retrieve the URL

tell application "Firefox" to set theURL to «class curl» of window 1

To paste the URL into the frontmost application you can use

tell application "Firefox" to set theURL to «class curl» of window 1
set the clipboard to theURL
tell application "System Events"
	tell (1st process whose frontmost is true)
		keystroke "v" using command down
	end tell
end tell

Thanks, StefanK.
Yesterday, I did try this script (or maybe a slight variant of this script). Anyway, today it is working well. If i run it from the script editor, it works properly and pastes the URL in the Script Editor window where my keyboard cursor is. However, setting a trigger for it does not work. I copied the script in the ~/Libary/Scripts/ folder. I tried setting up a hotkey with Quicksilver and I also tried running it by clicking it in the Scripts menu. Then I tried setting delay before the line which uses the keystroke and some other places as well. And finally I tried using this code.

and then

Nothing works. I dont get any errors. Just a beep. The reason for this is that the current application’s window loses focus when the script is running. Can you please try setting up a trigger and see if it is working?

I use FastScripts, which provides its own script menu and the ability to assign shortcuts.
The script works, if the application to paste the URL in is frontmost, FireFox and the script itself must not be frontmost.

Thanks for the prompt reply. Ok, I will see if I am going to switch to Fast Scripts. Can you point me to some documentation where i can get answer to my second query? I need it for other Quicksilver actions.

I don’t understand the second query.

You press a hotkey which calls a shell script which presses a QuickSilver-hotkey which calls a script.
That is a bit thought outside the box, isn’t it? :wink:

If you make sure, that the (Apple)script doesn’t become frontmost while executing, it will work

It is already working perfectly and not taking any noticeable extra time at all.

I just wanted to avoid going through three different scripts.


the shell script ( sends the output of the applescript (firefox.scpt) to Quicksilver’s first pane (this happens with the command " | qs " ). After that it executes the keystroke. Cmd+Shift+P is meant to paste the text that is in Quicksilver’s first pane. This is by default in Quicksilver.

got it :slight_smile:

I just found out that I dont have to switch to FastScripts.
This is what works for me (tested it for quite some time):
Script pasted in ~/Library/Scripts and trigger set using Quicksilver.

tell application "System Events"
	set {name:theApp, bundle identifier:theIdentifier} to (get 1st application process whose frontmost is true)
	set x to theApp
end tell

tell application "Firefox" to set currentURL to «class curl» of window 1
(*set myFirefox to properties of front window as list
set currentURL to item 3 of contents of myFirefox*)
set the clipboard to currentURL
tell application x
	tell application "System Events"
			tell process x
			delay 1
			keystroke "v" using command down
		end tell
	end tell
end tell

However, both <> and the properties method fail sometimes to get the URL.
Can an applescripter get a better entry into firefox to get the URL?

FireFox’s scriptability is hopeless, better use Safari.

Your additions to the script are actually not needed.
When you retrieve the 1st application whose frontmost is true, this application logically is activated and is frontmost

I know, but I have my own reasons for using Firefox.

I know you are right but I have problems believing it when I run my scripts especially the “frontmost” part of it. Though the application may be activated, it is not quite focused. I can see the faded color in the toolbar of the application indicating that it is not focused.

I have narrowed it down to this:

do shell script "osascript /Users/chris/Desktop/firefox.scpt | qs"
tell application "System Events" to keystroke "p" using {command down, shift down}

However, unless Quicksilver allows piping of text into itself, nothing more can be done. (As you see above, the result of the applescript can be piped into it though the result is text)

wake up, chris2 !! :lol: :smiley:
text can be piped into Quicksilver. Now, start working on important Quicksilver issues.

tell application "Firefox"
	set myFirefox to properties of front window as list
	set currentURL to item 3  of contents of myFirefox
		do shell script "echo " & currentURL & " | qs"
	tell application "System Events"
		delay 0.5
		keystroke "p" using {command down, shift down}
	end tell
end tell

One tip to those who are enslaved with some of Firefox’s plugins like me and cannot switch their browsers:
Whenever you have trouble scripting, look out for a Firefox plugin first. If you can’t find one, try harder.

There are plenty of plugins that do what i want and that do more than what I want regarding the URL issue i mentioned in this thread.

Some of them that I have just browsed through:

TinyURL Creator (this one is enough for me)
Shareaholic (overwhelming)
QuoteURLtext (simple and sweet)
and I know if a Firefox user sees this post of mine, he will add many more to this list.

(Ofcourse, the only difference between my need and TinyURL creator is that I will have to press the hotkey for TinyURL creator (it pastes the URL to clipboard by itself) before jumping on to the document, application etc where i want to paste it but i don’t mind it at all given the advantages of using Firefox. You can decide for yourself. :D)