# Using Applescript to open a file



## ewar (Oct 11, 2008)

Hi,

I'm trying to use a menu built in Adobe Flash to open a powerpoint file by calling an executable which I'm trying to write in applescript.

I've no idea how to do it, and I can't find any tutorials because it's probably such a simple command. When I have tried it with the following line of code:

tell application "Finder"
	activate
	select file "flyer2.ppt"
	open selection
end tell

it brings up a message saying "Finder cannot get file". I've tried being more specific as to the location of the file, but it's pretty much stabbing in the dark.

If you could tell me how to write this simple function which allows an executable file to open another file in its native software then I'd be most grateful.

Thanks,

Ewar


----------



## Martin Michel (Oct 11, 2008)

Hi Ewar,

You don't even need to bring up the finder to open files using AppleScript, it can be all done in the background with code like follows:


```
set filepath to POSIX path of "DandyDisk:Users:martin:Desktop:test.ppt"
try
	set command to "open " & quoted form of filepath
	do shell script command
end try
```

Hope that helps.


----------



## ejang (Apr 15, 2009)

Hi Martin,

the code that you posted works for opening files on your own computer very well; all you have to do is set the correct path. I too, wish to use applescript to open files. Suppose I made an application with applescript that has a second application nested inside its package contents. I know the application bundle is really just a folder, but I am having difficulty getting the script you supplied to open 


> set filepath to POSIX path of "(path to me):random application.app"
> try
> set command to "open " & quoted form of filepath
> do shell script command
> end try



How would I go about opening such a hidden file?


----------



## Martin Michel (Apr 15, 2009)

ejang said:


> How would I go about opening such a hidden file?



It's quite easy:


```
-- Mac path to a hidden sript file in the bundle
set hiddenscriptpath to (((path to me) as text) & "Contents:Resources:script.app")
-- Converting the Mac path to a Posix path
set hiddenscriptpath to POSIX path of hiddenscriptpath
-- Quoting the Posix path
set qtdhiddenscriptpath to quoted form of hiddenscriptpath
-- executing the script file
try
	set command to "open " & qtdhiddenscriptpath
	do shell script command
end try
```

Best regards from Berlin!


----------



## ejang (Apr 20, 2009)

Thank you very much! 

Speaking about the time, I want to be able to track a certain event when a certain people's log onto their computer for a little independent project of mine. Fortunately, iChat has a function where it can alert me by opening an applescript, but I want to  be able to generate a text log file where it displays the time of login. I have encountered two problems so far :L

First is the actual script. I know it shouldn't be that complicated, but I know how to acquire the date. What I want to do is to paste it into a text document or something of the sort. Here is what I have, but I don't know how I can save a result to the clipboard. In addition, I would prefer using a sticky note instead of TextEdit. 
Here is my faulty script.


> get (time string of (current date))
> set P to (result)
> copy (P) to clipboard
> tell application "TextEdit" to activate
> paste (P)



My second problem is actually getting iChat to open this script: it displays some error when i try it, although if I rename the extension to .applescript it works. 

Sorry for being such a computer-illiterate person


----------



## Martin Michel (Apr 21, 2009)

Hi ejang,

AppleScript can write into text files, no need to hijack TextEdit for this task. It's quite easy:


```
set curdate to (current date) as string
set logmsg to "LOGIN:" & tab & curdate & return
set logfilepath to (((path to desktop) as text) & "logins.txt")
set logsucess to my log2file(logmsg, logfilepath)

on log2file(logmsg, logfilepath)
	try
		set logfile to open for access logfilepath with write permission
		write logmsg to logfile starting at eof
		close access logfile
		return true
	on error
		try
			close access logfile
		end try
		return false
	end try
end log2file
```


If you want to read and set the clipboard, you can use this code:


```
-- setting the clipboard content
set myname to "Martin Michel"
set the clipboard to myname
-- reading the clipboard content
set clipboardcontent to the clipboard
```

But if you want to script TextEdit, you don't need to use the clipboard to paste text into a document:


```
set curdate to (current date) as text
tell application "TextEdit"
	activate
	if not (exists document 1) then
		set newdoc to make new document
	end if
	set text of document 1 to curdate
end tell
```



> it displays some error when i try it



Which error exactly?


----------



## mtmanner (Jul 30, 2013)

Hi Martin... I don't know if you're still active on this forum, but here goes:

I've tried your suggestion for using AppleScript to open a file, but it's not working. As a complete novice to AppleScript, I'm sure I have misunderstood how the syntax and grammar of the language work. Would you please advise me how to change this script --

set filepath to POSIX path of "Microsoft Word:Users:mtmannerocuments:Family JY & Friends:Craig:Craigs Goals:Socializing:Being with people.doc"
try
	set command to "open Users/mtmanner/Documents/Family JY & Friends:Craig/Craigs Goals/Socializing/Being with people.doc"
	do shell script command
end try

Thanks for your help.


----------

