Reading the MOTD msg of terminal In Mojave - terminal

I am creating an application just like terminal. For that i want to display the MOTD msg as its dispalyed in terminal on launch. How to get this msg in objective C / Swift.
Couldnt find a soultion for this in net. etc/motd file is not present in the system.
Thanks.

Per my knowledge the motd messages are generated by a bash script at /etc/motd.
So you basically need to execute the script either by Objective-C or Swift and grab the output
Objective C sample
Swift sample

Related

Mac OS X app running shell script but no terminal window

I am fairly new to Mac OSX, and am trying to create an .app file to run in the Applications folder. I'm using MacOS Big Sur, and this will just be run on a Mac (it doesn't need to be cross platform). There is a jar file that executes by running a shell script, as well as a few extra resource files, so ultimately I'd like to bundle this all together in something like a dmg so that I can share it easily with a few other people.
I followed the advice given here and here to set it all up, and almost everything works. The program starts when I double click on the .app file, but without a terminal window. Unfortunately I need the terminal window to open because I use it to log messages to the user.
Terminal is the default app for the shell script, and a terminal does open when I run the shell script directly by double-clicking on it. The script file works with an .sh extension and without one, though I get an error trying to run the .app if the script has an .sh extension. Everything has execute permissions. I went through the Info.plist docs but couldn't find anything about the Terminal. I also tried creating the .app with Automator, but with the same result.
Any suggestions would be very much appreciated, as at the moment I'm completely stuck. As I said, ultimately I want to have a way of sharing this with others who may not be very computer-savvy (e.g. they're used to just downloading things from the App Store and wouldn't be able to install things using the command line). So if I'm going about this all wrong or there's an easier way, then let me know that too.
Unfortunately I need the terminal window to open because I use it to log messages to the user.
If this is all you want the Terminal app for then you don’t need it all.
The Terminal app is a GUI app which runs a shell using standard OS calls, passes keyboard input to that shell (and hence any commands it in turns invokes) via a pipe, and reads the output of the shell (and hence...) and displays it in a window.
You can run your shell script direct from your own app, collect the output, and dimply that output in a window in your app.
In Objective-C the classes you want to look up are NSTask, to run a shell passing it your shell script, and NSPipe, to create pipes needed.
There are plenty of Q & A’s on SO about NSTask/NSPipe, here is one and here is another which uses Swift.
Note that both of the above read all of the output before converting it to a string which can then be displayed in a window or otherwise processed. This is not required and if you have a long running shell script and wish to display output as it runs you can read shorter chunks from the pipe. Read the documentation to see how to do this.
I'm posting my solution in case it helps anyone in the future. As the comments/answers said, what I really needed to know was how to get my app to open a terminal window. Obviously by creating the app manually (creating the folder structure and minimal Info.plist) I was missing some key elements.
I tried to generate one using Xcode. I'm sure it's pretty straightforward, but I got bogged down trying to work out the Swift code.
What worked for me was creating an AppleScript using Script Editor. The script simply tells the Terminal program to run my bash script:
tell application "Terminal"
do script "/Applications/{name of app}/Contents/MacOS/run.sh;exit"
end tell
The key is that Script Editor can save this as an app to the Applications folder, which means it creates the necessary folder structure and files. After that I could just copy my program files into the MacOS folder, which is where my bash script looks for everything.
One option might be to give the script file the extension .command, e.g., and then open that, e.g.:
open myscript.command
The myscript.command file needs execute permissions (chmod a+x myscript.command).
These .command files can also be double-clicked in Finder to execute them in a new Terminal window.

Connecting firefox "open with" with Automator script

I managed to make Firefox to follow specific protocol links (oxygen:/...) and use an application to open them. As the Mac OS X oXygen XML Developer app doesn't accept arguments, I need to use a shell script that is shipped together with oXygen itself. Given Firefox doesn't like shell scripts, I needed to write an Automator application 'Run Shell Script'. As I was having trouble to get it to work, I decided to use a stub code, with osascript, just to debug variables.
My current script looks like:
osascript -e 'display alert "'"$1"'"'
and it is defined as a bash script, receiving data as arguments.
When I click the link the dialog of osscript appears, but with an empty message. So, I am not sure how the URL is being passed to the application itself. I tried, also, with receiving data as stdin, but with no lock. I got to the point of writing a script that dumped all the parameters and STDIN to a file, but it ends up always empty.
Thank you for any hint.

How to let osascript not build applescript code which will not hit at runtime?

We have a script to send email using Microsoft outlook or Apple mail application. It will dynamically load the default email from system preference (maybe user input also), and using it to decide which mail client to use.
So the code is as following:
if (mailClientStr contains "outlook")
tell application id "com.microsoft.outlook"
-- <<< there will be error if there is no outlook installed
-- <<< even else branch will be run.
...
end tell
else
tell application id "com.apple.mail"
...
end tell
end if
On an machine which doesn't have outlook installed, and the mailClientStr will be "com.apple.mail", but this script cannot be run by osascript
It complains Can’t get application id "com.microsoft.outlook" even the first branch will not be executed. My understanding is osascript will need to access Outlook apple script interface when load and compile this script (before run it).
I can separate the outlook related code into a separate script, but because there is a lot of data to passing, it will be complex, so I don't want this workaround.
So does there any solution from the apple script language side?
From the AppleScript Language Guide:
Entering Script Information in Raw Format
You can enter double angle brackets, or chevrons («»), directly into a script by typing Option-Backslash and Shift-Option-Backslash. You might want to do this if you’re working on a script that needs to use terminology that isn’t available on your current machine—for example, if you’re working at home and don’t have the latest dictionary for a scriptable application you are developing, but you know the codes for a supported term.
You can also use AppleScript to display the underlying codes for a script, using the following steps:
Create a script using standard terms compiled against an available application or scripting addition.
Save the script as text and quit Script Editor.
Remove the application or scripting addition from the computer.
Open the script again and compile it.
When AppleScript asks you to locate the application or scripting addition, cancel the dialog.
Script Editor can compile the script, but displays chevron format for any terms that rely on a missing dictionary

xcode 4.6 applescript open text file for reading

I am trying to incorporate my applescript into xcode. the script works normally with applescript but not ran in xcode. I am trying to open the file for reading. here is the code
set Location to "US"
set DriverFile to "/Volumes/MacPrintDrivers/" & Location & "DriverInstall.txt"
set DriverInstallFile to POSIX file DriverFile
open for access DriverInstallFile
i have confirmed the file exists and i can display the contents. i can't seem to read this way in Xcode. the error i get is
«script» doesn’t understand the «event rdwropen» message. (error -1708)
ASOC (AppleScript Objective-C) has some problems with scripting addition commands (such as open for access). Sometimes you can work around this by saying tell current application to, e.g. tell current application to open for access. But for further details I suggest you get Shane Stanley's book: http://www.macosxautomation.com/applescript/apps/ He has explored this in great depth.

open console (terminal) window and execute command (rsync) on os x

I am from a windows background and trying to help a mac user friend to backup her pictures, docs, etc. onto an external drive. In windows, I would accomplish this by creating a simple batch file with an xcopy command and have a shortcut on the desktop that pointed to that .bat file when double clicked. However, in the mac world I am having significant trouble finding how to do this. I have searched repeatedly to find the mac equivalent, but all I find are sites saying things like "there are so many options on a mac - use one of them." However, none have ever given a specific solution nor pointed to a specific solution. Anyone here know of a specific step by step process to accomplish this? I simply want to be able to have her double click an icon on the desktop and have it copy her personal documents (not application settings or other overhead) to her external hard drive. Any help would be appreciated.
Create the batch file, which is usually called a shell script.
Enter all the commands that you want to run.
Set the executable bit, this is done with chmod +x path-to-the-file in Terminal.
Show info for the script and set Terminal to the application which should open it.
However, what I've done in similar situations and that I would recommend that you do is that I've created a shell script and instead of using Terminal I've initiated it from an AppleScript application. You can of course embed the entire shell script in the AppleScript as well. Basically it will look something like the following:
on run
do shell script "rsync -av ~/Pictures /Volume/Backup"
end run
Repeat the do shell ... line for each folder that you want to copy, or call the shell script itself. Then use AppleScript Editor which is included with Mac OS X and save it as an actual application.

Resources