Applescript & Clipboard

itsjustkatie

macrumors newbie
Original poster
Jun 20, 2008
16
0
Hi everyone,

I am trying to write an applescript where I can drop something onto a droplet, get it to copy the file path, open the shell, navigate to that directory, then have it create a text file on my desktop of the listed files. I have both parts figured out separately, but I cannot figure out how to bridge the two and get what's on the clipboard into the shell after the "cd" command.

Here's my script as it stands right now:

on open {dropped_item}

tell application "Finder" to set the clipboard to the dropped_item as text

(do shell script "cd" and paste) the clipboard

do shell script "ls */* | cat>>~/Desktop/filelist.txt"

end open


The part in bold is the part I can't figure out: taking the clipboard and pasting is into the shell after the "cd" command. Any help appreciated!
 

itsjustkatie

macrumors newbie
Original poster
Jun 20, 2008
16
0
I have something else now that I'm trying to add on to this script. (Not that I figured out how to copy the clipboard into the terminal yet - still need help there) After all of the tasks in my above post are complete, I would like to open up the text file and select all and copy.

I know how to open the file that I want to open:

tell application "TextEdit"
activate
open ":Users:katie:Desktop:filelist.txt"
end tell

But, I do not know how to make it copy everything to the clipboard in that open file. Any help is wonderful! Thanks
 

itsjustkatie

macrumors newbie
Original poster
Jun 20, 2008
16
0
Going to continue talking to myself here :) in hopes somebody may help, or maybe this will help somebody else!

I figured out how to make Text Edit copy the text in a specified file to the clipboard, here it is:

tell application "TextEdit"
activate
open ":Users:katie:Desktop:filelist.txt"
tell application "System Events" to keystroke "a" using {command down}
tell application "System Events" to keystroke "c" using {command down}
set selecTxt to the clipboard as text

tell application "TextEdit"
quit

end tell


I still need to figure out how to paste a filepath into Terminal after the "cd" command and all my problems will be solved...well for today anyway.
 

mysterytramp

macrumors 65816
Jul 17, 2008
1,335
3
Maryland
First, using System Events should be the last resort.

Second, TextEdit provides plenty of AppleScript goodness to do things. Try this:

Code:
tell application "TextEdit"
	set x to the text of document 1
end tell

set the clipboard to x
 

mysterytramp

macrumors 65816
Jul 17, 2008
1,335
3
Maryland
The part in bold is the part I can't figure out: taking the clipboard and pasting is into the shell after the "cd" command. Any help appreciated!
Although the clipboard is a central part of working with the Mac, you won't need it very often in AppleScript because you can save data to variables, which can then be passed around your script, to various applications and routines, etc. As you can see below, rather than pasting a file path in the Terminal window, you can just construct a string to pass to "do shell script"

Here's a start:

Code:
set hfsFolderPath to choose folder

set posixFolderPath to quoted form of POSIX path of hfsFolderPath

set listOfFiles to (do shell script "ls " & posixFolderPath)
Once you have listOfFiles, you can write it to disk, or pass it to TextEdit, or whatever.

(Sorry, I didn't write this as a droplet because of time.)

mt
 

itsjustkatie

macrumors newbie
Original poster
Jun 20, 2008
16
0
Thanks for all of the great info, mt. I played around with the variables a bit and found this to work (mostly) for the part I couldn't figure out.

Code:
on open dropped_item
	
	get the POSIX path of dropped_item
	
	do shell script "ls " & the quoted form of result & " > ~/Desktop/filelist.txt"
	
end open
But now, the only problem is that it only lists the first level of folders and/or files from the dropped file. So them I tried

Code:
on open dropped_item
	
	get the POSIX path of dropped_item
	
	do shell script "ls */* " & the quoted form of result & " > ~/Desktop/filelist.txt"
	
end open
And I get error messages:
ls: broadcasthost: Operation timed out
ls: localhost: Operation timed out
ls: agentx: Permission denied
ls: backups: Permission denied
ls: root: Permission denied

That command line works when input into the shell, so I'm not sure where I am going wrong. Would love your help again if you have a moment. Thanks!
 

mysterytramp

macrumors 65816
Jul 17, 2008
1,335
3
Maryland
Try the -R switch with ls.

Code:
do shell script "ls -R " & the quoted form of result & " > ~/Desktop/filelist.txt"
The alternative would be to break off the cataloging code and use the Finder to recursively walk through your folders. The advantage is that you'll have more control over what is cataloged. The disadvantage is you might not want that advantage.

mt
 

itsjustkatie

macrumors newbie
Original poster
Jun 20, 2008
16
0
YES! -R totally worked. I'm surprised I had forgotten about that. Thanks so much for the help, this actually garners the result that I was looking for. Thanks again mt.
 

Similar threads

Register on MacRumors! This sidebar will go away, and you'll see fewer ads.