Applescript to Copy from Clipboard to Terminal

Joined
Dec 10, 2009
Messages
2
Reaction score
0
Points
1
I need to figure out how to write a line in an applescript that will take what's on the clipboard (will be a file path) and paste it into the Terminal after the "cd" command. Then it will list out all of the files in that file path and save it as a txt file.

Right now, it looks a little something like this and I get the error is "Cant get keystroke "cd" of application "Terminal"

on open {dropped_item}

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

tell application "Terminal" to keystroke "cd"

tell application "System Events" to keystroke "v" using {command down}

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

end open

OR If I use this script, I get this error: "AppleEvent handler failed"

on open {dropped_item}

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

open application "Terminal"

do shell script "cd" and pastefromclipboard

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

end open

I also remember seeing something to the effect of "Can't make 'cd' into boolean" but I cannot find that version of my script at the moment.

Any help with this would be great. Thanks!
 
Joined
Dec 13, 2007
Messages
256
Reaction score
10
Points
18
Location
United States of America
Your Mac's Specs
2.1GHz MacBook with 4GB RAM, Mac OS X 10.6, iLife and iWork ‘09
So what you're trying to do is output the contents of a folder dropped onto the application? If so, here's an approach that will work:
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
This code only has one greater than symbol; use two if you want the file to continue to grow rather than being replaced.

Hope that helps.
 
OP
K
Joined
Dec 10, 2009
Messages
2
Reaction score
0
Points
1
Thanks nabl! Yes, I am trying to get a list of all of the files and folders within the folder dropped onto the application.

This worked perfectly to grab the top level of the folders within the folder I wanted it to list. So now, my issue is getting it to list everything within those initial folders...

I tried using this:

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 am getting error messages that read:
ls: broadcasthost: Operation timed out
ls: localhost: Operation timed out
ls: agentx: Permission denied
ls: backups: Permission denied
ls: root: Permission denied

The command line "ls */* | cat>>~/Desktop/filelist.txt" works fine in the shell every time and gets me what I want, so I'm not quite sure what those errors are relating to.

Is there another way to generate all of the file within the subfolders of the dropped folder?

Thanks again so much, this is big help so far!
 
Joined
Dec 13, 2007
Messages
256
Reaction score
10
Points
18
Location
United States of America
Your Mac's Specs
2.1GHz MacBook with 4GB RAM, Mac OS X 10.6, iLife and iWork ‘09
Try this:
Code:
on open dropped_item
	get the POSIX path of dropped_item
	do shell script "ls -pR " & the quoted form of result & " > ~/Desktop/filelist.txt"
end open
The -p switch adds a slash at the end of results that are folders (for example, "folder/" instead of "folder"), and the -R switch tell ls to recursively output the contents of each folder it comes across. For more options, type man ls at the command line.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top