applescript subroutine and action's properties - applescript

I am stuck with an action properties error with my little applescript. Here's the script:
tell application "evernote" to activate
tell application "System Events"
--tell process "evernote"
set the clipboard to text_content()
keystroke "v" using command down
keystroke return
--end tell
end tell
return input
end run
here's sub-routine after main program
on text_content()
delay 1
tell application "Safari" to set theURL to URL of front document
set theDate to do shell script "date +'%d-%m-%Y'' - '%T"
set theusertext to " , "
set get_text to (theURL & return & theusertext & theDate) as string
return get_text
end text_content
This is showing me an action properties error. I've tried to define get_text as properties at different places all over the script, but still getting the same error. Any guidance on how to deal with it?

This works for me in Evernote 6.6.1 (453372) on OSX 10.11.4, executing from either the Scripts menu or from Script Editor.
--- GET THE INFO FROM SAFARI FIRST ---
set the clipboard to (text_content() & return)
tell application "Evernote" to activate
tell application "System Events"
tell process "evernote"
keystroke "v" using command down
delay 0.5
--- Evernote is NOT behaving properly ---
-- It should leave the cursor at the END of the paste
-- but it is not.
-- So, we have to arrow down then issue a return
-- The above delay is also required to make sure the paste
-- has completed before any more keystrokes
key code 125 -- DOWN arrow
keystroke return
end tell
end tell
on text_content()
--delay 1 ## don't see any need for this
tell application "Safari" to set theURL to URL of front document
set theDate to do shell script "date +'%d-%m-%Y'' - '%T"
set theusertext to " , "
set get_text to (theURL & return & theusertext & theDate) as string
return get_text
end text_content

Related

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

How to get filename to activated application within applescript?

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

applescript to close pdf window

I have an applescript that looks like this:
repeat
tell application "Adobe Reader"
open "filepath/name.pdf"
end tell
delay (60)
tell application "Adobe Reader"
open "filepath/name1.pdf"
end tell
delay (60)
tell application "Adobe Reader"
open "filepath/name2.pdf"
end tell
delay (60)
end repeat
I want to be able to close pdf windows after they have been opened. The issue is that these pdfs reside on a share and users have the ability to update them. The script will only display the updated pdf if it is stopped and restarted. I do not want to have to do this manually. How can I do this?
Here's a solution, which makes Preview scriptable and then continues to open and close a file of your choice.
-- http://www.macworld.com/article/1053391/previewscript.html
on addAppleScriptFeatures()
try
tell application "Finder"
set the Preview_app to (application file id "com.apple.Preview") as alias
end tell
set the plist_filepath to the quoted form of ¬
((POSIX path of the Preview_app) & "Contents/Info")
do shell script "defaults write " & the plist_filepath & space ¬
& "NSAppleScriptEnabled -bool YES" with administrator privileges
do shell script "chmod -R 755 " & the quoted form of (POSIX path of the Preview_app) with administrator privileges
return true
on error myErr number MyNr
display dialog myErr & " (" & MyNr & ")." buttons {"OK"} default button "OK" with icon 0
return false
end try
end addAppleScriptFeatures
if addAppleScriptFeatures() then
set f to choose file
tell application "Preview"
activate
open f
delay 5 -- short for testing
close window 2
end tell
end if

applescript: how to automatically choose to save a safari document at a PARTICULAR PATH with system events

I want to make a script that will save safari documents using system events (command+s) and I want the file to always be saved at a particular path. (I could make a habit of always saving safari files at a certain folder but that's not a robust solution) How can I make sure system events saves a document at a certain path?
Directly from this MacRumors forum post:
tell application "Safari"
activate
end tell
tell application "System Events"
tell process "Safari"
click menu item "Copy" of menu "Edit" of menu bar 1
end tell
end tell
delay 1
set the_filename to (the clipboard) & ".html"
set the_filepath to "Macintosh HD:Users:Roy:Documents:" & the_filename
set the_shellfilepath to "'/Users/Roy/Documents/" & the_filename & ".download'"
set the_shellfilepath2 to "'/Users/Roy/Documents/" & the_filename & "'"
tell application "Safari"
activate
save document 1 in the_filepath
end tell
do shell script "mv " & the_shellfilepath & " " & the_shellfilepath2
Set the path (the_shellfilepath) as appropriate.
Changing the folder of a file dialog:
try
try -- this would result in an error when there's no clipboard
set old to the clipboard
end try
-- delay 1 -- for testing
-- activate application "Safari"
tell application "System Events"
keystroke "s" using command down
keystroke "g" using {shift down, command down}
delay 0.1
set the clipboard to "~/Movies/"
delay 0.1
keystroke "av" using command down
delay 0.1
keystroke return
delay 0.1
end tell
set the clipboard to old
end try
Saving to a specific folder with a partially encoded URL as a file name:
tell application "Safari"
set x to URL of document 1
set r to do shell script "echo " & quoted form of x & " | sed 's|/$||;s|:|%3A|g;s|/|%2F|g'"
do shell script "curl " & x & " > " & quoted form of ((system attribute "HOME") & "/Desktop/" & r & ".html")
end tell
Saving to a specific folder but choosing the file name manually:
tell application "Safari"
set x to URL of document 1
set answer to text returned of (display dialog "" default answer ".html")
do shell script "curl " & x & " > " & quoted form of ((system attribute "HOME") & "/Desktop/" & answer)
end tell

AppleScript Focus / Rename File (and clicking anywhere)

on run {input}
set filepath to POSIX path of input
do shell script "touch " & quoted form of filepath & "untitled"
return input
end run
Is what I have so far, and it works, but is there a way to then focus on the file then trigger a rename? I dont want the rename to be automatic, just trigger the event (like pressing "return" while you have a file selected). And I dont want to use any sort of modal...
Quick Side question: is there a way to set this so that i dont have to select a folder or file directly, but can do it by, lets say, clicking in a white space in a folder as long as it's in Finder? Right now I have my "Service receives selected" to "files or folders" in Finder.app.
== UPDATED CODE ==
on run {input}
set filepath to POSIX path of input
do shell script "touch " & quoted form of filepath & "untitled"
tell application "Finder"
activate
set target of Finder window 1 to POSIX file "/Users/oscargodson/Documents/designs/untitled"
end tell
tell application "System Events"
tell process "Finder"
keystroke return
end tell
end tell
return input
end run
If i hardcode the path it works! But how do I get it as a var that works?
Here's one way. I think a modal window where you ask for the name would be better but you can try this. Notice you do not use "POSIX path" in this code. Applescript doesn't use POSIX paths. Also {input}, as indicated by the brackets around it, is a list of items. Therefore you act on the items of the list, and in this case we act on the first item.
set filepath to item 1 of input
tell application "Finder"
activate
reveal filepath
end tell
tell application "System Events"
tell process "Finder"
keystroke return
end tell
end tell
EDIT: With your updated code, here's a working script...
on run {input}
if (class of input) is not list then set input to {input}
set theFolder to (item 1 of input) as text
try
alias theFolder
tell application "Finder"
if (class of item theFolder) is not folder then error "input is not a folder."
activate
set theFile to make new file at folder theFolder with properties {name:"untitled"}
reveal theFile
end tell
delay 0.2
tell application "System Events"
tell process "Finder"
keystroke return
end tell
end tell
on error theError number errorNumber
tell me
activate
display dialog "There was an error: " & (errorNumber as text) & return & return & theError buttons {"OK"} default button 1 with icon stop
return
end tell
end try
return input
end run
tell application "Finder"
activate
reopen -- in case there are no open windows
set target of Finder window 1 to POSIX file "/Applications/Safari.app"
end tell
reveal and select always open a new window, set target and set selection don't.
I don't know why, but when set selection it used in column view, you can only select items in the entire contents of the target of the front window. The same thing doesn't happen in other views, so it seems like a bug.
Fix for the code in the edited question:
on go(input)
set p to POSIX path of (input as text)
set p2 to p & "untitled"
do shell script "touch " & p2
tell application "Finder"
reopen
activate
set target of Finder window 1 to POSIX file p2
end tell
delay 0.3 -- time to release modifier keys
tell application "System Events" to keystroke return
end go
tell application "Finder"
set fold to folder (path to documents folder)
end tell
go(fold)
(That on go and the last lines are just for testing.)
I've created an AppleScript based on the #regulus6633's one, but with some improvements.
Note: This answer was originally posted as an AskDifferent answer. I'm copy/pasting here for convenience.
The idea is to create an Automator workflow and assigning a shortcut to it using the following steps:
Open Automator and create a Service;
Set the input to no input, and the application to Finder.app;
Drag and Drop the Run AppleScript workflow element onto the grey space;
Put the contents of this AppleScript in the textbox;
Save the workflow with a reasonable name (like New File);
Go to Settings -> Keyboard -> Shortcuts -> Services and assign a shortcut to it.
Now, let's show the AppleScript:
set file_name to "untitled"
set file_ext to ".txt"
set is_desktop to false
-- get folder path and if we are in desktop (no folder opened)
try
tell application "Finder"
set this_folder to (folder of the front Finder window) as alias
end tell
on error
-- no open folder windows
set this_folder to path to desktop folder as alias
set is_desktop to true
end try
-- get the new file name (do not override an already existing file)
tell application "System Events"
set file_list to get the name of every disk item of this_folder
end tell
set new_file to file_name & file_ext
set x to 1
repeat
if new_file is in file_list then
set new_file to file_name & " " & x & file_ext
set x to x + 1
else
exit repeat
end if
end repeat
-- create and select the new file
tell application "Finder"
activate
set the_file to make new file at folder this_folder with properties {name:new_file}
if is_desktop is false then
reveal the_file
else
select window of desktop
set selection to the_file
delay 0.1
end if
end tell
-- press enter (rename)
tell application "System Events"
tell process "Finder"
keystroke return
end tell
end tell
For convenience, I'm putting this AppleScript in this GitHub Gist.

Resources