Sunday, October 07, 2018

Run app on iOS 12.0 device from Xcode 9.4

Recently I updated my iPhone 5s with iOS 12.0. To my surprise, Xcode 9.4 on my Mac High Sierra dose not support iOS 12.0 device. That's hold on my app development.

My first reaction was to install the newest Xcode 10 beta 2 from Apple. The whole package is pretty big, 5GB+. That's OK. I downloaded the package and installed it. After that I was rushing to run my project in Xcode 10 (I did make a copy of the project to an external HD to run, to avoid any mass up). There were not much updates I had to make change in my app codes, however, I got a compiling error.

That was really painful time. The error seems to be related to linkage to iOS frameworks, no thing to do with my codes. I could not figure out what's wrong in Mac library system. Even worse, I could not run Xcode 9.4 with my project neither, with the similar linkage error.

I tried to look into my Mac library and Xcode framework dependencies. Remove and re-add frameworks. Nothing was working. I decided to stop further trying or mass-up. Took a break.

Fortunately, before I installed Xcode 10 beta 2, I checked in all my codes into my code-repository. I had my Mac Time Machine on. I know what is the time when I had the correct and latest working version of my Xcode 9.4, and it was just one day before. By using TM restoration, I finally got my app, in about 3 hours restoration of my Mac, working in Xcode 9.4.

With the successful restoration, I followed an advice from SO, copying iOS 12.0 supported folders from Xcode 10 beta to my Xcode 9.4.


Xcode-beta/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport


To


/Applications/Xcode/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport

Now I can run my codes from Xcode 9.4 to my iPhone 5s device.

What's adventure and life-saving experience!


References


Read More...

Wednesday, October 03, 2018

Python script: MapIt.py


Based on an example, script of mapIt.py, from the video of Automate the Boring Stuff with Python, I figured out that the codes should be changed in Mac OS X, as I mentioned that there are multiple versions of python in Max OS in my previous blog.

In addition to those changes, there are some steps that should done in Mac to make the script as executable in terminal. I further make it working in Spotlight as well. Here are my notes.

Script

Based on the video, I made minor changes on the script. Here is what I have now:


  1. #! /usr/bin/env python3
  2. import webbrowser, sys, pyperclip
  3. sys.argv  # example ['mapit.py', '870', 'Valencia', 'St.']
  4. address = 'Home'
  5. if len(sys.argv) > 1 :
  6.    address = ' '.join(sys.argv[1:]) # ['870', 'Valencia', 'St.']
  7. else :
  8.    address = pyperclip.paste()
  9. # https://www.google.com/maps/place/<address>
  10. webbrowser.open('https://www.google.com/maps/place/' + address)

The first line is important if you want to run the script directly from terminal or Spotlight. Still there are some other steps to be done in order to run the script directly from terminal or Spotlight.

Notice that I specify the name of python is python3! Another minor change is the default address value: Home. I am using Mac and the Google map knows where my home is in my local Mac account.

I saved the script at my local user account path: ~/Programming/py/mapit.py With script ready, I test the script in terminal:

...$ python3 Programming/py/mapit.py Park Ave 83 St New York

As soon as I type in the above command, a new tab page of Google Map is opened in Safari with the correct address.



Make the Script Executable

So far so good. However, I would prefer to open the script directly from terminal, or from Spotlight, without specifying python3. For the case of terminal, first I have to make the script executable with the following commands:

...$chmod a+x ~/Programming/py/mapit.py

The second step is to add my script path to env PATH so that I can simply type in mapit.py anywhere in command line. This is done by adding the path in ~/.bash_profile:

  1. PATH="/Library/Frameworks/Python.framework/Versions/3.7/bin:${PATH}:/Users/dchu/Programming/py"
  2. export PATH

With above setup ready, now I can run the script anywhere in terminal:

...$ mapit.py Donggaodi Hongxing St Beijing

Isn't that cool?




Make the Script Runnable in Spotlight


Spotlight on Mac is a very convenient way to launch an app, to open a file, and to find information. However, unlike Windows Run, it not the way to direcly launch an app with arguments. Even though I set my script mapit.py as executable, I cannot run it from Spotlight. It will only open the script in Xcode!

One way to get it work in Spotlight is to change the script to .command extension. Mac OS will recognize this type of file as a runnable command.

I prefer to keep my script as .py. In UNIX or Mac, I can create an alias file of script: hard link(only hard link works in Spotlight). Here is the command to set it up:

  1. ...$ cd ~/Programming/py
  2. ...$ ln mapit.py mapit.command

The good thing is that any change to my script will be automatically reflected in the command file.

In order to open the Google Map with specified address, I need to copy the address to clipboard first. Then I open my Spotlight and type in mapit.command. It will pick up the address and open Google map in my default browser.

The only thing is that I cannot specify address directly as arguments in Spotlight. This is the limitation of it, and I cannot find a way to do that.

This is why I need to use pyperclip package in my script.




Note: another minor thing has to be done to make it runnable cleanly. The command is running in terminal. When the Spotlight launches the command and opens the map in browser, it will leave the terminal in open status. I prefer to close the terminal automatically.

To do that, go to terminal Preferences..., change the shell setting to Close the Windows:




The last word I have to say is that even the python script is very powerful and convenient to open a map with an address. I think Mac's Automator is much more powerful. It is easy to create (as a service), and can be set to selected text to open a map. Even though, I think it is a very good practice and great programming experience.

References


Read More...