I need to write a simple applescript that will be opened with an argument. That argument will be a file path. I need it to save this file path to a text file. I want to avoid using shell script.
I'm finding this surprisingly difficult, despite having done it in .sh and .bat already
Thanks very much!
Adam
//// the current code which is not working is below. the code begins with "on run" and ends with "end run" but for some reason this isn't being highlighted as code
on run argv
set this_PATH to (POSIX file argv)
tell application "TextEdit"
activate
make new document
set text of document 1 to this_PATH as text
save document 1 in "/Users/adamparkinson/Desktop/date.txt"
end tell
end run
I made this work:
#!/usr/bin/osascript
on run argv
tell application "TextEdit"
activate
make new document
tell document 1
set its text to (item 1 of argv as text)
save in posix file (item 1 of argv as text )
end tell
end tell
end run
I called it like this:
./SavePath.sh /Users/Me/Desktop/junk/Newstuff.txt (Of course I used chmod u+x SavePath.sh to make it executable.)
If this works for you, then please check of my answer as answered. :)
with applescript it is very easy to create application Drag and Drop without any script shell below a version Drag and Drop of your script
on open draggedItems
set this_PATH to quoted form of POSIX path of draggedItems
repeat with currentItem in draggedItems
tell application "TextEdit"
activate
set doc to open POSIX file "/Users/adamparkinson/Desktop/date.txt"
make new word at end of text of doc with data (this_PATH as text) & return
end tell
end repeat
end open
Related
I'm trying to get AppleScript to select a file, but I'm getting an error when I execute the script.
Here's the code
tell application "System Events"
set a to "/Users/me/files/"
set fileName to "myFile.jpg"
set thePath to POSIX path of a
tell application "Finder"
set selection to fileName of thePath
end tell
keystroke "c" using command down
end tell
I'm getting an error "Can’t get POSIX path of "/Users/me/files/"
Essentially, what I'm trying to do is find a way to select a file so that I can copy it for later. But I want to copy the actual file, not the path of the file. The idea is to create a service that copies the file so that I can paste it into another application easily.
If there's a better way to do this, then please let me know
These following 2 lines of code would copy your file to the clipboard. This would only work with a single file. Not multiple items.
activate
set the clipboard to POSIX file (POSIX path of (choose file))
Scratching my head after reading lots of different threads on this and tried a bunch of scripts but none seem to work.
I'd like to use Automator to automate Word 2016 conversion of a selection of docx files to pdf.
Used the following Automator Service:
Used the following script:
on run {input, parameters}
tell application id "com.microsoft.Word"
activate
open input
set doc to name of active window
set theOutputPath to (input & ".pdf")
save as active document file name theOutputPath file format format PDF
end tell
end run
Which results in error: Microsoft Word got an error: active document doesn’t understand the “save as” message.
The main issue is that input is a list. You have to use a repeat loop to process each file separately
I added a line to close the current document after having been converted
on run {input, parameters}
tell application id "com.microsoft.Word"
activate
repeat with aFile in input
open aFile
set theOutputPath to ((aFile as text) & ".pdf")
tell active document
save as it file name theOutputPath file format format PDF
close saving no
end tell
end repeat
end tell
end run
To prevent the problem discussed in #vadian's answer, save the file first to Word's default folder (that's usually ~/Library/Containers/com.microsoft.Word/Data/Documents) and then move the file somewhere else.
on run {input, parameters}
repeat with aFile in input
tell application "System Events"
set inputFile to disk item (aFile as text)
set outputFileName to (((name of inputFile) as text) & ".pdf")
end tell
tell application id "com.microsoft.Word"
activate
open aFile
tell active document
save as it file name outputFileName file format format PDF
close saving no
end tell
set defaultPath to get default file path file path type documents path
end tell
tell application "System Events"
set outputPath to (container of inputFile)
set outputFile to disk item outputFileName of folder defaultPath
move outputFile to outputPath
end tell
end repeat
return input
end run
I'm trying to create an Automator droplet that allows me to pass one or more video files to an Applescript that then saves out reference files with "_ref.mov" appended to the file name by using QuickTime Player 7.
Saving as a reference is the default save method, which is why I'm not using save reference file function (also, because I can't find any docs on the using descriptors attribute object.)
I found this script on another forum, and it works, but it doesn't accomplish exactly what I need because it requires that 1) a file is open and 2) the user manually specifies an output file name.
on run {input, parameters}
tell application "QuickTime Player 7"
activate
try
if not (exists document 1) then display dialog "please open a quicktime movie." buttons {"cancel"} default button 1 with icon 1
set theFile to (choose file name)
save document 1 in theFile
close document 1
end try
end tell
return input
end run
I've tried to make changes to accomplish what I need, but my changes break the script:
on run {input, parameters}
tell application "QuickTime Player 7"
activate
try
open input
set theFile to ((path of input) & "_ref.mov))
save document 1 in theFile
close document 1
end try
end tell
return input
end run
I've also tried:
set theFile to (((path of input) & "_ref.mov") as text) doesn't work
either does as alias or as string
Not sure where I'm going wrong. Please help if you can!
What would be a simplest way to change a current working directory (cwd) using AppleScript. It would be equivalent to OS's cd or Python's os.chdir. Would be great if a destination directory if doesn't exist would be created on a fly (but that would be highly optional).
If you are looking to use with an applications save dialog...
set filePath to "path/to/my/file"
tell application "System Events"
-- Once save dialog is open
keystroke "g" using {shift down, command down}
delay 1
set value of text field 1 of sheet 1 of sheet 1 of window 1 to filePath
end tell
You might be thinking of doing something like this:
tell application "Finder"
# ^^^ or whatever application you want to control up there
# to get to a file...
set filePath to POSIX file "/Users/username/Documents/new.mp3"
# to get to a folder...
set testFolder to POSIX path "/Users/username/test"
if (exists (folder testFolder)) then
say "folder exists"
else
make new folder at testFolder
endif
end tell
I'm basing my answer off the answers on this page and this related question (or this one).
I am trying to write some automation code (primarily in Ruby Selenium). At some point, a file chooser is opened in Safari so that the user can select a file for upload. Selenium cannot handle this, but I think AppleScript should be able to. I am new to AppleScript and haven't been able to find any boilerplate code of someone automating a file chooser dialog. I'm reading through the AppleScript docs, but any ideas would be most helpful.
Some more searching and I found a great answer here: Applescript file dialog with UI scripting
Here's what I ended up using:
on run argv
tell application "Safari"
activate
-- Usage check
set argc to count argv
if argc is not greater than 0 then
return "Usage: SafariFileChooser file_name [window_name]"
end if
-- The file we will choose to open
set file_name to item 1 of argv
-- Flip to the named window, if specified
if argc is equal to 2 then
set window_name to item 2 of argv
set flip_count to index of window window_name
repeat (flip_count - 1) times
activate
tell application "System Events" to keystroke "`" using command down
end repeat
end if
-- Interact with the dialog using System Events (thanks mcgrailm)
tell front window
activate
tell application "System Events"
keystroke "g" using {shift down, command down}
keystroke file_name
delay 1
keystroke return
delay 1
keystroke return
end tell
end tell
end tell
return 0
end run
Another option I just discovered is to specify the directory using the command-line:
do shell script "defaults write com.apple.Safari NSNavLastRootDirectory /path/to/directory"
This way you can do slightly less in UI scripting. Run this command before you open the file chooser, and it will put you into the directory specified. Include all the files you need in this directory, and you can just script command+a to select them all, and return.