How to send dot in AppleScript - macos

I want to upload a file using AppleScript in Chrome. The file path contain a dot (.), but this script is not able to send the dot to 'Go to the folder:'.
on run argv
tell application "System Events"
tell application "Google Chrome" to activate
delay 2
log argv
keystroke "g" using {command down, shift down}
delay 1
keystroke item 1 of argv
end tell
end run
I am running the script with this command:
nsadmins-Mac:util nsadmin$ osascript macUploadChrome.scpt "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py"
This text appears in the "Go to the folder" field after running this script:
/System/Library/Frameworks/Pythonframework/Versions//lib/python/socketpy

That's weird, but what if you try using copy/paste instead:
set the clipboard to (item 1 of argv as string)
keystroke "g" using {command down, shift down}
delay 1
keystroke "v" using {command down}

Why Chrome? Honestly curious, I never have any problems with Safari. Anyways,
--set filepath to POSIX path of theimage
--^^set filepath to the desired file path^^
delay 2
tell application "System Events"
activate application "Safari"
delay 0.5
keystroke "g" using {shift down, command down} --open goto
set the clipboard to filepath
keystroke "v" using {command down}
delay 0.7
keystroke return -- enter goto text
delay 0.4
keystroke return --press enter on file
end tell
Maybe that filepath is just too long. I doubt it though. Try using this, if it doesn't work with Chrome try it with Safari just to see if that's the problem. This is meant to be run when the "file choosing window" is already open, but the full script that I wrote is a bit more extensive, let me know if you want to see it.

Related

In MacOSX, can't get applescript+automator service working to send text from any application to "Sublime Text 3"

Want to send text from any application to "Sublime Text 3" and this is how I am thinking of doing it
Create an "automator" service
Set a shortcut for it
Call shortcut from app
I am able to create the "automator" service and it works fine when tested from within "automator" (by means of the "Get Text" action)
But, when I trigger shortcut from application, "Sublime Text" does not open new tab with the selected text (and does nothing)
This is how I am setting up the "automator" service
Service receives selected text in any application
Copy to Clipboard
Run this applescript
on run {input, parameters}
tell application "System Events"
set frontmost of process "Sublime Text" to true
tell application "System Events" to keystroke "n" using command down
tell application "System Events" to keystroke "v" using command down
end tell
end run
Appreciate the help, thanks
The following example AppleScript code works for me:
on run {input, parameters}
tell application "Sublime Text" to activate
delay 1
tell application "System Events"
keystroke "n" using command down
delay 0.5
keystroke "v" using command down
end tell
end run
Note that the value of the delay commands may or may not need to be adjusted for timing on your system.

Applescript - Keystroke text from variable

I have a file that contains some text. I am able to read this text into a variable. I want to use the keystroke command to send this text to an application. When the line containing the keystroke runs, it produces dialog box that contains an error 'Syntax Error - Can't get keystroke "..content of file.." '
on run {input, parameters}
set inputfile to "/path/to/textfile.txt"
set fileText to read inputfile
keystroke fileText
end run
How can I send the content of a file as keystrokes?
The keystroke command is part of the Process Suite of System Events, therefore when using the keystroke command, it must come from System Events.
Example:
tell application "System Events" to keystroke fileText
However, that said, you need to first set focus to where you want it to be typed, e.g.:
tell application "TextEdit" to activate
delay 1
For example, your code would be:
on run {input, parameters}
set inputfile to "/path/to/textfile.txt"
set fileText to read inputfile
tell application "TextEdit" to activate
delay 1
tell application "System Events" to keystroke fileText
end run

Run emacs using applescript and open file

What I would like to do is have an automator script open a file in emacs when I drag a file onto it, and bring terminal to the front.
Right now, when I try to do this with input set to ~/Desktop/test.txt, it either opens up the main page, or it ends up with ~/Desktop/testtxt. Am I doing this wrong? Is there a better way to do this?
This is what I have right now:
set thePath to POSIX path of input
tell application "Terminal" to do script "emacs"
tell application "System Events"
tell process "Terminal"
activate
delay 2
keystroke "x" using control down
delay 1
keystroke "f" using control down
delay 1
keystroke thePath
delay 1
keystroke return
delay 1
end tell
end tell
return input
end run
Use the file path as an argument to emacs
on run {input}
tell application "Terminal"
repeat with i in input
do script "emacs " & quoted form of POSIX path of i
end repeat
activate
end tell
return input
end run

Run a command in a new tab in terminal

I'm trying to write a script that could run several commands in several tabs in the terminal.
I found a lot of informations about it, but it doesn't work as I want it to. So I probably need AppleScript.
• This code run a command in a new window:
tell app "Terminal"
do script "echo hello"
end tell
• And this one open a new tab
tell application "Terminal"
activate
tell application "System Events"
keystroke "t" using {command down}
end tell
end tell
But I didn't figured out how to "mix" them. Any idea ?
tell application "Terminal"
activate
tell application "System Events"
keystroke "t" using {command down}
end tell
do script "echo hello" in selected tab of the front window
end tell

Applescript to capture window

I am trying to create an applescript to capture a window. The keyboard shortcut is cmd+shift+4 then space. I am not able to use this in applescript.
My code :
tell application "system events"
keystroke "21, 49" using {command down, shift down}
end tell
It doesnt work. The problem with the scrip it using space bar. I need to hold cmd, shift & 4 and then press space bar.
Try:
tell application "System Events"
keystroke (ASCII character 36) using {command down}
delay 1
keystroke space
end tell
This Applescript might work better for you rather than using GUI scripting
it uses the screen capture command line. for more info look at the screencapture Man page
set fileName to do shell script "date \"+Screen Shot %Y-%m-%d at %H.%M.%S.png\""
tell application "System Events" to set thePath to POSIX path of desktop folder
do shell script "screencapture -W " & "\"" & thePath & "/" & fileName & "\""
If all you want is to get to that point then use this:
tell application "System Events"
key code 21 using {shift down, command down}
delay 0.1
key code 49
end tell

Resources