I have a applescript with the following:
on open dir
tell application "Finder"
tell application "Terminal"
activate
tell application "Terminal"
do script "cd " & dir
end tell
end tell
end tell
end open
It works, and gets the directory thats dropped on it, but it cd's into cd Macintosh HD:Users:USER:Desktop:C Files:
Is there any way to replace : with / ? in applescript?
Found the answer myself!:
set dir to quoted form of POSIX path of dir
Related
I'm trying to launch a Finder window of a folder that's in the same directory as my script. When I run the code below, it launches a blank Finder window set to the path of the script not to the folder.
tell application "Finder"
tell application "Finder" to make new Finder window
set file_path to (path to me) as text
set target of Finder window 1 to file_path
end tell
How can I get the path to the folder of the script, not the script?
You were close. You do not need the text version of the file, you only need the file itself, then you can ask Finder for that file's container:
tell application "Finder"
tell application "Finder" to make new Finder window
set file_path to (path to me)
set target of Finder window 1 to file_path's container
end tell
The shortest way I know to do this is:
tell application "Finder" to open ((path to me as text) & "::")
Editing your script renders the following:
tell application "Finder"
make new Finder window -- There is no need for an your second tell statement
set file_path to (path to me as text) & "::" -- Goes up one directory
set target of Finder window 1 to file_path
end tell
How would I create an icon that would open the terminal into a specific directory? Could also be into the directory where the icon lives.
You can use "Automator" this way:
Create an automator application
Add a "Run Script Applescript" action
Modify the code to:
on run {input, parameters}
tell application "terminal"
activate
set UnixPath to POSIX path of ((path to me as text) & "::")
do script with command "cd " & UnixPath
end tell
end run
Save it
Run it! Move it where you want, Run it...
Finally found the easiest answer to this: use Finder services. See http://www.howtogeek.com/210147/how-to-open-terminal-in-the-current-os-x-finder-location.
I am creating an applescript that opens terminal, I am dragging a file onto the script (using automater) and want the directory/name of file to be fed into my terminal application
on run {input, parameters}
tell application "Terminal"
activate
do script with command "qpdf --qdf arg1 output.pdf"
end tell
end run
You don't need to use Automator. Save this script as an application instead...
on open of droppedItems
tell application "Terminal"
if not (exists window 1) then reopen
activate
end tell
repeat with anItem in droppedItems
set itemPath to POSIX path of anItem
tell application "Terminal"
do script "qpdf --qdf " & quoted form of itemPath & " output.pdf" in window 1
-- do script "echo " & quoted form of itemPath in window 1
end tell
end repeat
end open
I´m trying to create a keyshortcut to open terminal in current folder. Looking around, I found this code to create a service (the part of adding the shortcut to this service is solved), only added things are the "; clear" and some of the "activate" so it shows
on run {input, parameters}
tell application "Finder"
activate
set myWin to window 1
set theWin to (quoted form of POSIX path of (target of myWin as alias))
tell application "Terminal"
activate
tell window 1
activate
do script "cd " & theWin & ";clear"
end tell
end tell
end tell
return input
end run
It is not working as i would like.
troubles:
it opens two windows in terminal, have no idea why. It has nothing to
do with the added "activate"… it has always donde that
if I select an item on finder ( a folder ) it opens its parent directory and i would
like it to open the selected folder
this is my very first try with Applescript so if the error is obvious i just can't see it
Thanks in advance
The do script command already opens a window in Terminal. Try it this way:
tell application "Finder" to set theSel to selection
tell application "Terminal"
set theFol to POSIX path of ((item 1 of theSel) as text)
if (count of windows) is not 0 then
do script "cd " & quoted form of theFol & ";clear" in window 1
else
do script "cd " & quoted form of theFol & ";clear"
end if
activate
end tell
I like the reopen approach better...
tell application "Finder" to set currentFolder to target of front Finder window as text
set theWin to currentFolder's POSIX path
tell application "Terminal"
if not (exists window 1) then reopen
activate
do script "cd " & quoted form of theWin & ";clear" in window 1
end tell
How does one open a file in the same folder as the AppleScript code? Something along these lines?
tell application "QuickTime Player"
activate
open "file.avi"
end tell
(which doesn't work).
Thanks!
tell application "Finder"
open file "somefile.txt" of folder of (file (path to me))
end tell
(only works once you've saved the script - otherwise "path to me" goes to the script editor)