I suspect this is a bit cd Desktop. You can try:
(cd ~/Desktop; sh code.sh)
However:
You must make an executable code.sh, so you do not need to call it with sh. This is done using chmod 0755 code.sh.
script ( , script), script, ~/Desktop/code.sh
#!/bin/bash
dir=$(dirname $0)
cd $dir
:
➜ ~ cat tmp/code.sh
#!/bin/bash
dir=$(dirname $0)
cd $dir
ls -l
➜ ~ chmod 0755 tmp/code.sh
➜ ~ tmp/code.sh
total 64
drwxr-xr-x 6 andy staff 204 Feb 22 18:53 Archives
drwxr-xr-x 11 andy staff 374 Jun 18 13:59 DerivedData
-rw-r--r-- 1 andy staff 225 May 20 13:44 MyFirstProgram.X
-rwxr-xr-x 1 andy staff 3072 May 20 13:44 MyFirstProgram.exe
drwxr-xr-x 3 andy staff 102 Jan 6 2014 bug_reports
-rwxr-xr-x 1 andy staff 43 Aug 6 14:15 code.sh
-rw-r--r-- 1 andy staff 11539 May 20 08:33 iOS_Team_Provisioning_Profile_.mobileprovision
-rw-r--r-- 1 andy staff 1438 May 20 08:40 ios_development.cer
-rwxr-xr-x 1 andy staff 272 Aug 5 08:55 script.sh