Run AppleScript everytime a file is opened.

Hello.

This is just a sketch:

You could make an applet, which you use as a default application for this particular workbook.
Then you could do all the stuff in the applets open handler. Including having excel opening
the workbook.

I’m not 100% sure that this will work, but 100% sure that something close will work (Eventually).

Best Regards

McUsr

mikerickson wrote

Came across a Ruby script (athttp://lists.apple.com/archives/applesc . 00280.html) that seems to overcome the issue of only activating when a file is double-clicked.

I do not know anything about Ruby nor have I any idea why or how it works.

The script below will activate the script if the file is double clicked to open OR if the file is opened via the File menu.

I have changed the file to an Excel Workbook (2008)

I have left the idle return at 1 second.

So far it activates the dialog every time the file is opened whether from the File menu or by double clicking.

File Name: My New Excel Workbook.xlsx

File Location: /Users/mikerickson/Documents/My Special Folder/My New Excel Workbook.xlsx

on idle
    set tscript to "ruby -e 'puts File.stat(\"/Users/mikerickson/Documents/My Special Folder/My New Excel Workbook.xlsx\").atime'"
    set RubyDate to do shell script tscript
    set MO to {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
    repeat with i from 1 to count of items in MO
        if item i in MO = text 5 thru 7 of RubyDate then
            set mn to i as number
        end if
    end repeat
    tell (current date)
        set its day to 1 -- Overflow precaution.
        set its year to text -4 thru end of RubyDate
        set its month to mn
        set its day to text 9 thru 10 of RubyDate
        set its hours to text 12 thru 13 of RubyDate --  . coercions.
        set its minutes to text 15 thru 16 of RubyDate
        set its seconds to text 18 thru 19 of RubyDate
        set theDate to its date (date string) -- A date string in the user's preferred format.
    end tell
    if theDate > (current date) - 1 then
        activate me
        display dialog "Do whatever you like from now on"
    end if
    return 1
end idle

Val

Model: iMac8,1
AppleScript: Version 2.3 (118)
Browser: Safari 533.16
Operating System: Mac OS X (10.6)

I have a few files which I want to open in a defined way.

To achieve that, I use a script which grabs the list of files available in the folder containing them.
Then I select the file in a choose from list dialog.
So it’s easy to do the wanted tasks before opening the file (or after opening it).


set mon_dossier to "Western 2:Important:iWork:documents_Numbers:"

tell application "System Events"
	set les_documents to get name of every file of folder mon_dossier
end tell

set mon_document to (choose from list les_documents)
if mon_document is false then error number -128
set mon_document to mon_dossier & item 1 of mon_document
(*
here code for tasks to achieve before opening the document
*)
tell application "System Events"
	set nom_visible to displayed name of disk item mon_document
end tell

tell application "Numbers"
	open mon_document
	repeat until exists document nom_visible
		delay 0.2
	end repeat
end tell

(*
here code for tasks to achieve after opening the document
*)

Yvan KOENIG (VALLAURIS, France) lundi 21 juin 2010 22:38:18

Hello.

I have even another solution that I haven’t implemented.

A folder action which opens the file Info window when the file (or an alias?) is dropped onto (added) to the folder.
You then choose which applet should open it, which provides the “on open” functionality you want eventually.:slight_smile:

There could be said a lot about this approach as an opposite to Yvan’s method. Yvan’s is more well documented.
But If he had several solutions in one file, there could be more code to maintain. My solution is more easygoing.
As I don’t have to test for filetypes and such. On the other, If all these files were accessed through a folder then
it wouldn’t make such a mess. And if I provided a test in the “Associated” droplets for which path the files were opened from, I could even be informative about who really is opening the document if it was opened from it’s
original path, or bypass the extra functionality all together.

But I could always open the document directly,

Best Regards From a hot Summer Night in Southern Norway

McUsr

Yvan’s solution at post 23 would appear to be the wisest so far on the thread as it is certain that it will work every time.

While I can confirm that, so far on my machine, the ruby script picks up the file opening every time, I would be more confident if I knew where it gets its information from.

There must be accessible depositories of system information that are very useful but that applescript or spotlight cannot use.

The more experienced scripters on the forum might enlighten us.

In the meantime I have tinkered with my script and replaced the repeat with an offset. It still works ----on my machine.

on idle
	set tscript to "ruby -e 'puts File.stat(\"/Users/mikerickson/Documents/My Special Folder/My New Excel Workbook.xlsx\").atime'"
	set RubyDate to do shell script tscript
	set MO to "Jan01Feb02Mar03Apr04May05Jun06Jul07Aug08Sep09Oct10Nov11Dec12"
	set a to offset of (text 5 thru 7 of RubyDate) in MO
	set mn to text (a + 3) thru (a + 4) in MO
	tell (current date)
		set its day to 1 -- Overflow precaution.
		set its year to text -4 thru end of RubyDate
		set its month to mn
		set its day to text 9 thru 10 of RubyDate
		set its hours to text 12 thru 13 of RubyDate -- . coercions.
		set its minutes to text 15 thru 16 of RubyDate
		set its seconds to text 18 thru 19 of RubyDate
		set theDate to its date (date string) -- A date string in the user's preferred format.
	end tell
	if theDate > (current date) - 1 then
		activate me
		display dialog "Do whatever you like from now on"
	end if
	return 1
end idle

Cheers,

Val

This has been a good thread with lots of solutions.

@Val: Your ruby solution is almost the same as my find -name theName -mtime 1s
Both commands perform the stat system call, and checks the -accessed -date attribute of the inode for the file.

I am not after any “Best” solution really, it is however important to spot the differences among them. Your ruby solution may be faster than my find -atime solution, but then again my find solution takes care of all the current date stuff for me.

Best Regards

The Procastinating McUsr :cool:

Here is an enhanced version.

(1) it use Choose file so you may navigate to select the file to open.
This allow a wider range of file types.

(2) the script may be used as a droplet (if saved as an application packege (application under 10.6.x)

(3) look at the workaround required by a Leopard’s bug.


(*
edit permitted to fit your needs
*)
property permitted : {"com.apple.iwork.numbers.numbers", "com.apple.iwork.numbers.sffnumbers", "com.apple.iwork.numbers.template", "com.apple.iwork.numbers.sfftemplate", "com.apple.iwork.pages.pages", "com.apple.iwork.pages.sffpages", "com.apple.iwork.pages.template", "com.apple.iwork.pages.sfftemplate"}

--=====

on run
	(*
	set mon_dossier to "Western 2:Important:iWork:documents_Numbers:"
	
	tell application "System Events"
		set les_documents to get name of every file of folder mon_dossier
	end tell
	
	set mon_document to (choose from list les_documents)
	if mon_document is false then error number -128
	
	my do_your_duty(mon_dossier & item 1 of mon_document)
	*)
	
	(*
There is a bug in Leopard so it is unable to apply a filter accepting iWork documents
*)
	if 5 = (system attribute "sys2") then
		set _ to choose file without invisible
	else
		set _ to choose file of type permitted without invisible
	end if
	my do_your_duty(_ as text)
end run

--=====

on open (sel)
	local un_document, _
	set un_document to (item 1 of sel) as text
	tell application "System Events"
		set _ to (class of disk item un_document) as text
	end tell
	if _ is not in {"file", "file package", "«class cpkg»"} then
		if my parle_anglais() then
			error "Selection is « " & _ & " ». It must be a file or a package !"
		else
			error "La sélection est de type « " & _ & " ». Elle devrait être fichier ou paquet !"
		end if
	end if
	my do_your_duty(un_document)
end open

--=====

on do_your_duty(mon_document)
	local type_identifier, nom_visible
	(*
Grab two useful properties
*)
	tell application "System Events" to tell disk item mon_document
		set type_identifier to type identifier
		set nom_visible to displayed name
	end tell -- System Events
	(*
Check that the file's type identifier is a permitted one.
*)
	if type_identifier is not in permitted then
		if my parle_anglais() then
			error "The file "" & nom_visible & "" isn't of type :" & return & my recolle(permitted, return)
		else
			error "Le fichier « " & nom_visible & " » n'est pas de type :" & return & my recolle(permitted, return)
		end if -- parle_anglais
	end if -- type_identifier
	
	if type_identifier starts with "com.apple.iwork.numbers." then
		(*
here code for tasks to achieve before opening the Numbers document
*)
		my open_it(mon_document, nom_visible, "Numbers")
		(*
here code for tasks to achieve after opening the Numbers document
*)
	else if type_identifier starts with "com.apple.iwork.pages." then
		(*
here code for tasks to achieve before opening the Pages document
*)
		my open_it(mon_document, nom_visible, "Pages")
		(*
here code for tasks to achieve after opening the Pages document
*)
	else if type_identifier starts with "yourChoice's identifier" then
		(*
here code for tasks to achieve before opening the yourChoice's document
*)
		my open_it(mon_document, nom_visible, "yourChoice")
		(*
here code for tasks to achieve after opening the yourChoice's document
*)
	end if
end do_your_duty

--=====

on open_it(le_document, nom_du_document, the_app)
	tell application the_app
		open le_document
		repeat until exists document nom_du_document
			delay 0.2
		end repeat
	end tell -- the_app
end open_it

--=====

on parle_anglais()
	return (do shell script "defaults read 'Apple Global Domain' AppleLocale") does not start with "fr_"
end parle_anglais

--=====

on recolle(l, d)
	local oTIDs, l
	set oTIDs to AppleScript's text item delimiters
	set AppleScript's text item delimiters to d
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

--=====

I feel that using a droplet is more efficient and versatile than using a folder action script.

Yvan KOENIG (VALLAURIS, France) mardi 22 juin 2010 15:21:05

Hello.

Nice! Yvan :slight_smile: Ssshh! You are about to be looted.

I agree with you. The only folder action I want is the one which associate a file in a folder for the purpose of some customized opening action with a droplet like yours.
All I want the folder action to do is to bring up alternatives of droplets with actions.
( Could be well organized by having those kinds of droplets in a special folder).

Best Regards

McUsr

The droplet is more efficient than a folder action script for this task.

I use folder actions daily for several tasks which can’t be automated with droplets.

As I switch often between 10.4 and 10.5, I use one disk for each system and a third one to store datas.
I never took time to check if I may define target recipients on the third volume so,

(1) the downloaded items are received in a folder of the startup volume then a folder action script move them to the third volume

(2) the screenshot are sent in a folder of the startup volume then they are moved on the third volume.

I use other folder actions for these tasks:

(1) I grab from ebay photos of the potteries made in my workshop which appear in auctions.
I gather these photos in one folder per object then submit the folder to a folder action script which rename the embedded files with a fixed pattern.
(2) before embedding screenshots when I respond in Apple Discussions forums, I drop them in a folder to reduce their width to 640 pixels if needed.

but they will be replaced soon by droplets.

Each tool as pros and cons. The problem is that often I built a script in a hurry and discover later that I didn’t made the good choice.

Sometimes, representants aren’t re-elected,
sometimes couples divorce .
sometime French soccer money-minded are playing the fool.

It’s just life.

Yvan KOENIG (VALLAURIS, France) mardi 22 juin 2010 18:22:06

For completeness sake I revisited McUsr’s post 19 and came up with this (only slightly changed) version of the script of that post.

on idle
	set parentfolder to path to documents folder
	set myfile to parentfolder & "My Special Folder" & ":" & "My New Excel Workbook.xlsx" as text
	set ppath to quoted form of POSIX path of myfile
	if (do shell script " find  " & ppath & " -atime -1s") is not "" then
		activate me
		display dialog "Do whatever you like from now on"
	end if
	return 1
end idle

It works, though Yvan’s appears a more certain way of controlling a whole process.

% cpu 1.2
real memory 9mb
virtual memory 28mb

Cheers from a disappointed?? Ireland.

Val

Hello.

The greatness of AppleScript is being “glue” between applications, and like glue, you can bend it (“her”) to your will,
-switch freely between osascripts, applets (foreground and background), scripts, droplets, folder action scripts.:slight_smile:

It is a rarity to end up with the best solution at the first time. At least if you are me. I often find that I want it different than what I perceived when I made it, and often the solution makes such a shift - to another kind of applescript. The folder action to droplet shift is something I have done a couple of time. Either because that in the end it was more practical than the folder action, or led to far less coding.

Apropos and most definitively out of the thread scope :wink:

I have recently (also) discovered the convert command. But I also have discovered the enscript command which means that I can bypass CUPS-PDF in many cases and get a real filename too.

Best Regards

McUsr

I would support a FIFA petition to suspend France for Brasil 2014 and let Ireland participate without qualification

I would support it too !

Yvan KOENIG (VALLAURIS, France) mardi 22 juin 2010 19:28:15

As I had some minutes available, I looked at one of my folder actions scripts and discovered that converting it in a droplet required less than one minute .
Edit this line:


on adding folder items to this_folder after receiving added_items

as


on open (these_items)
	tell application "Finder"
		set this_folder to (container of (item 1 of these_items)) as alias
	end tell

Of course it’s the quick and dirty soluce.
Next step will be to replace calls to Finder by calls to System Events or Shell scripts.

Yvan KOENIG (VALLAURIS, France) mardi 22 juin 2010 19:35:28

A third non-scripting post in a row!

We risk being expelled from the class!

Interesting different cultural responses (from media anyway)

Ireland: Disappointment. GB:Glee France: Outrage (i think) Scripters:Solidarity with the underdog

Val

Hello.

Two years later, I found the solution, to this, I have really known it for a long time, just haven’t realized it.

you use the teqnique described in this post: http://hints.macworld.com/article.php?story=20091013114424722 But you use the UtI’s or Uri’s for your file in question. Having done so, you assign that particular file to that application, that has been registered, to take that kind of files, via the “Info” panel for it in Finder.

And best of all, there is no performance overhead in this.

Then you should be all good!

Have a nice Summer! :cool: