Change Applescript into single line NSApplescript source - cocoa

Im building an app for learning purposes of using applescript to send multiple commands during an action. Below is the code I'm messing with but I have stripped out the actions in between the "" and replaced them with numbers. Everything works fine in applescript but making this into an NSApplescript initwithsource: line has been a bother.
tell application "Terminal"
activate
set currentTab to do script "1"
do script "2" in currentTab
do script "3" in currentTab
do script "4" in currentTab
delay 0.5
tell application "Finder" to set visible of process "Terminal" to false
end tell
What is the best way to combine this applescript into a single line? Thanks!

"What is the best way to combine this applescript into a single line?"
Use AppleScript? :-D
First, in AppleScript Editor, open the Preferences window and click the option to Show Script menu in menu bar.
Then choose Open Scripts Folder from the script menu item up in the upper right of the screen.
Create a new AppleScript .scptd document with the following script:
tell application "AppleScript Editor"
set string_ to text of first document
-- make a list with each line of the script
set stringLines to paragraphs of string_
set originalDelims to AppleScript's text item delimiters
-- add newlines
set AppleScript's text item delimiters to "\\n"
-- now combine the items in the list using newlines
set stringNewlines to stringLines as string
set AppleScript's text item delimiters to "\""
set stringNewlines to text items of stringNewlines
set AppleScript's text item delimiters to "\\\""
set stringNewlines to stringNewlines as string
set AppleScript's text item delimiters to originalDelims
set stringNewlines to "#\"" & stringNewlines & "\""
set the clipboard to stringNewlines
end tell
(Note that this script isn't perfect: it works fine for simple scripts like the one you provided, but isn't able to convert itself).
Save that as a script in the Scripts folder you revealed earlier.
Then open your script document you want to convert and make it the front document in AppleScript Editor. Then invoke the conversion script by choosing it from the Script menu.
Given the script you provided, it should produce the following constant NSString:
#"tell application \"Terminal\"\n activate\n set currentTab to do script \"1\"\n do script \"2\" in currentTab\n do script \"3\" in currentTab\n do script \"4\" in currentTab\n delay 0.5\n tell application \"Finder\" to set visible of process \"Terminal\" to false\nend tell\n"

Related

How can I add a Finder Service that would use a single selected file, perform a shell script on it then display it as a dialog window?

How can I add a Finder Service that would use a single selected file, perform a shell script on it then display it as a dialog window?
I currently have Automator receiving files and folders within Finder then running an Applescript with the following:
on run {input, parameters}
tell application "Finder"
set filename to selection
set filename to quoted form of POSIX file filename
set theCMD to "/usr/local/bin/exiftool "
set theScript to theCMD & filename
set theResult to do shell script theScript
display dialog theResult
end tell
end run
I continuously receive errors. The intention is to show a dialog window with Exif metadata information for a single selected file within a Finder window. I used brew to install exiftool to retrieve the data.
I'm new to applescript and can't figure out how to get this to work. Any help is appreciated.
An Automator service passes input items to the workflow, so you don't need to do anything else to get the selection. In a Run AppleScript action the input parameter is a list, so you should pick a specific item or just loop though all the items:
on run {input, parameters}
repeat with anItem in the input
set anItem to quoted form of POSIX path of anItem
set theResult to (do shell script "/usr/local/bin/exiftool " & anItem)
display dialog theResult
end repeat
# return input
end run

Error when replacing characters applescript

I am trying to make a script that executes a runnable Jar file in the form of an applescript application. I'm not very experienced in applescript, so I mostly went off of online snippets. The first problem I ran into was that the directory automatically formats as using a ":" instead of a slash (ex. "usr:bin" instead of "usr/bin") so I found something that's supposed to replace the : with a /. This, however, produces an error every time.
I've found little on how to fix my error and the stuff I found was very specific to those cases and didn't make much sense to me.
This is my code:
tell application "Finder"
set current_path to container of (path to me) as alias
end tell
set AppleScript's text item delimiters to ":"
set currPath to text items of current_path
set AppleScript's text item delimiters to "/"
set current_path to currPath as string
set AppleScript's text item delimiters to {""}
currPath
tell application "Terminal"
activate
set currentTab to do script ("cd " & current_path)
set currentTab to do script ("java -jar Inoisulcnoc Pre-Alpha 2")
end tell
The error I get every time says:
error "Can’t get every text item of alias \"Macintosh HD:Users:knotsnappy:Desktop:Inoisulcnoc Pre-Alpha 2:\"." number -1728 from every text item of alias "Macintosh HD:Users:knotsnappy:Desktop:Inoisulcnoc Pre-Alpha 2:"
How should I be able to fix this?
You'll want to change the standard path into a POSIX path.
tell application "Finder"
set current_path to (POSIX path of (container of (path to me) as alias))
end tell
tell application "Terminal"
activate
set currentTab to do script ("cd " & current_path)
set currentTab to do script ("java -jar Inoisulcnoc Pre-Alpha 2")
end tell
The delimiters, etc. are unneeded once you have the correct syntax.

AppleScript with multiple finder inputs

THE WHAT AND WHY
From inside Finder I want to be able to make hidden any files/folders, regardless if sudo is required or not, by a simple right click.
STEPS TAKEN ALREADY:
After a bunch of experimenting I settled on an Automator service running an AppleScript
on run {input, parameters}
set filehide1 to {}
repeat with filehide2 in input
set end of filehide1 to POSIX path of filehide2
end repeat
do shell script "chflags hidden " & quote & filehide1 & quote with administrator privileges
end run
ISSUE:
So the script works peachy BUT in it's present form only when one item at a time is selected. How do I tweak Automator / AppleScript to work regardless if its 1 file or 500?
You can't pass an AppleScript list to the shell. You have to flatten the list and separate the file paths by space characters. This can be done with text item delimiters.
The script escapes each file path in the repeat loop so the quotes are not needed in the do shell script line.
on run {input, parameters}
set filehide1 to {}
repeat with filehide2 in input
set end of filehide1 to quoted form of POSIX path of filehide2
end repeat
set {TID, text item delimiters} to {text item delimiters, space}
set fileList to filehide1 as text
set text item delimiters to TID
do shell script "chflags hidden " & fileList with administrator privileges
end run

How to reveal default Safari downloads folder through Applescript?

I am new to AppleScript and would like to know how to reveal the default downloads folder in Safari through AppleScript.
--- WHAT I HAVE ALREADY TRIED ---
set filePath to do shell script "defaults read com.apple.Safari DownloadsPath"
do shell script "open " & quoted form of filePath
The above script to my understanding will set the variable "filePath" to Safari's default PList entry in preferences.
This works great as long as the location is NOT somewhere within the user home folder. If its within a user folder, the Log shows me a "~/" before the path with no reference to anything prior to the user home folder (relative path)
How do i accomplish my goal? is there a way to get the absolute path to the folder? Or perhaps an alternative method?
Based on #WilliamTFroggard's answer:
tell application "Finder"
set folderPath to do shell script "defaults read com.apple.Safari DownloadsPath"
if folderPath = "~" or folderPath starts with "~/" then ¬
set folderPath to text 1 thru -2 of POSIX path of (path to home folder) & rest of characters of folderPath
open POSIX file folderPath as alias
activate
end tell
The text element is used to strip the trailing / from the home folder (/Users/johndoe/).
The rest property returns every character following the ~ in folderPath (~/Downloads).
/Users/johndoe + /Downloads = /Users/johndoe/Downloads.
You can do this by Applescript GUI Scripting to make Applescript click the 'show downloads' option from the 'view' menu:
tell application "Safari"
activate
end tell
tell application "System Events"
tell process "Safari"
tell menu bar 1
tell menu bar item "view"
tell menu "View"
click menu item "Show downloads"
end tell
end tell
end tell
end tell
end tell
Here's a method using Python:
do shell script "python -c \"import os; print os.path.expanduser('`defaults read com.apple.Safari DownloadsPath`')\""
If you really want the long AppleScript method, try this:
set filePath to ""
set AppleScript's text item delimiters to "/"
set filePathWords to words of (do shell script "defaults read com.apple.Safari DownloadsPath")
if item 1 of filePathWords is "~" then
set item 1 of filePathWords to (POSIX path of (path to home folder as string))
set filePath to filePathWords as string
else
set filePath to "/" & filePathWords as string
end if
If that second "/" in the path bothers you (it sort of bothers me...), you can use this instead:
set filePath to ""
set AppleScript's text item delimiters to "/"
set filePathWords to words of (do shell script "defaults read com.apple.Safari DownloadsPath")
if item 1 of filePathWords is "~" then
set item 1 of filePathWords to (do shell script "cd ~ && pwd")
set filePath to filePathWords as string
else
set filePath to "/" & filePathWords as string
end if
I think what you want is this:
tell application "Finder"
activate
open ("/Users/yourname/Downloads" as POSIX file)
end tell
Just replace "yourname" with your name in Finder.

Applescript - save argument to text file

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

Resources