image
image

Go Back   macosx.com > Mac Help Forums > Mac OS X System & Mac Software

Reply
 
LinkBack Thread Tools
  #1  
Old October 11th, 2008, 08:00 AM
Registered User
 
Join Date: Oct 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
ewar is on a distinguished road
Using Applescript to open a file

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
Reply With Quote
  #2  
Old October 11th, 2008, 09:24 AM
Registered User
 
Join Date: Sep 2008
Location: Bielefeld & Berlin, Germany
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Martin Michel is on a distinguished road
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:

Code:
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.
Reply With Quote
  #3  
Old April 15th, 2009, 02:23 PM
Registered User
 
Join Date: Feb 2009
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
ejang is on a distinguished road
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
Quote:
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?
Reply With Quote
  #4  
Old April 15th, 2009, 03:25 PM
Registered User
 
Join Date: Sep 2008
Location: Bielefeld & Berlin, Germany
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Martin Michel is on a distinguished road
Quote:
Originally Posted by ejang View Post
How would I go about opening such a hidden file?
It's quite easy:

Code:
-- 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!
Reply With Quote
  #5  
Old April 20th, 2009, 07:58 PM
Registered User
 
Join Date: Feb 2009
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
ejang is on a distinguished road
Thanks!

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.
Quote:
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. :P

Sorry for being such a computer-illiterate person
Reply With Quote
  #6  
Old April 21st, 2009, 10:00 AM
Registered User
 
Join Date: Sep 2008
Location: Bielefeld & Berlin, Germany
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Martin Michel is on a distinguished road
Hi ejang,

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

Code:
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:

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:

Code:
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
Quote:
it displays some error when i try it
Which error exactly? :-)
Reply With Quote
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



All times are GMT -5. The time now is 09:40 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Copyright 2000-2010 DigitalCrowd, Inc.